为ggplot定制换行符

时间:2018-04-20 21:08:30

标签: r ggplot2

我可以使用qicharts2包创建质量控制图。

library(tidyverse)
library(qicharts2)    
(plot1 <- qic(age,
              data    = tail(cabg, 100), 
              chart   = 'i',
              ylab    = 'Years',
              xlab    = 'Patient #'
              )
)
p1 <- plot1$data 

然后我可以自定义图表。

(plot2 <- ggplot(p1, aes(x, y)) +
    geom_ribbon(ymin = p1$lcl, ymax = p1$ucl, fill = "black", alpha = 0.05) +
    geom_line(color = "black", size = 1) + 
    geom_line(aes(x, cl)) +
    geom_point(color = "black" , fill = "black", size = 2) +
    geom_point(data = p1 %>% filter(sigma.signal == TRUE), color = "red", size = 2) + 
    ggtitle(label = NULL) +
    labs(x = NULL, y = NULL) +
    scale_y_continuous(breaks = seq(0, 100, by = 10)) + 
    coord_cartesian(ylim = c(0, 100)) + 
    theme_bw() + 
    theme(
      text = element_text(size = 18), 
      axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6), 
      axis.text.y = NULL, 
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      strip.text.x = element_text(size = 14, color = "black", angle = 0))
)

在我的qichart中使用part参数会导致它在指定的部分点分割。

(plot3 <- qic(age,
              data    = tail(cabg, 100), 
              chart   = 'i',
              part    = c(70, 85), 
              ylab    = 'Years',
              xlab    = 'Patient #'
)
)
p3 <- plot3$data 

我需要在下面添加到我自定义的ggplot2语法中,以便以相同的方式分享它?我所拥有的就是一切,除了它,它不像上面的语法那样。

(plot4 <- ggplot(p3, aes(x, y)) +
    geom_ribbon(ymin = p3$lcl, ymax = p3$ucl, fill = "black", alpha = 0.05) +
    geom_line(color = "black", size = 1) + 
    geom_line(aes(x, cl)) +
    geom_point(color = "black" , fill = "black", size = 2) +
    geom_point(data = p3 %>% filter(sigma.signal == TRUE), color = "red", size = 2) + 
    ggtitle(label = NULL) +
    labs(x = NULL, y = NULL) +
    scale_y_continuous(breaks = seq(0, 100, by = 10)) + 
    coord_cartesian(ylim = c(0, 100)) + 
    theme_bw() + 
    theme(
      text = element_text(size = 18), 
      axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 0.6), 
      axis.text.y = NULL, 
      panel.grid.major = element_blank(), 
      panel.grid.minor = element_blank(), 
      strip.text.x = element_text(size = 14, color = "black", angle = 0))
)

1 个答案:

答案 0 :(得分:2)

以下情节是您要找的吗?

如果是这样,我使用的是在= geom_ribbon和geom_line的美学中的group =

enter image description here

PerformTask()