首页 > 解决方案 > 如何在 ggplot2 中的刻度标签之间的 x 轴下方放置垂直线以及如何以灰色/白色交替构面背景颜色

问题描述

我有以下情节:我想在 de x 轴标签之间放置线条(请参阅第二个情节以了解我想要的红色内容)。我还想让第一个面的背景为灰色,第二个为白色,依此类推。

到目前为止,这是我的代码:

ggplot(DCE,aes(x=Level,y=Marginal))+
  geom_point()+
  geom_line(aes(group=Attribute), size=1)+  ## connect subjects with a line
  #facet_grid(.~Attribute, scale="free")+        ## 1 row of panels by roost
  facet_grid(.~Attribute, scale="free", switch = "x")+        ## indien je de facet labels onder wilt
  theme_bw()+
  zero_margin+                        ## squash together
  theme(axis.text.x = element_text(angle = 90, hjust=0.95,vjust=0.2))+
  geom_errorbar(aes(ymin=DCE$marginLow, ymax=DCE$MarginHigh), width=.2, size=0.8,
                position=position_dodge(0.05))+
  theme(strip.background =element_rect(fill="white",colour = "white", size = 1))+
  theme(strip.text = element_text(colour = 'black'))+
  theme(strip.placement = "outside")+
  geom_hline(yintercept=0, 
             color = "grey", size=0.5)

这是我的情节: 在此处输入图像描述

这就是我想要的(不同刻度的红线): 在此处输入图像描述

这是我的数据集的 str() :

> str(DCE)
tibble [13 x 6] (S3: tbl_df/tbl/data.frame)
 $ Attribute : chr [1:13] "5-year progression free survival" "5-year progression free survival" "5-year progression free survival" "5-year progression free survival" ...
 $ Level     : Factor w/ 13 levels "*50%","60%","65%",..: 1 2 3 4 5 6 7 9 8 10 ...
 $ Marginal  : num [1:13] 0 0.12 0.17 0.26 0 0.08 0 -0.11 -0.02 0 ...
 $ marginLow : num [1:13] NA 0.07 0.14 0.23 NA 0.05 NA -0.14 -0.06 NA ...
 $ MarginHigh: num [1:13] NA 0.17 0.21 0.3 NA 0.1 NA -0.07 -0.01 NA ...
 $ subject   : num [1:13] 1 1 1 1 2 2 3 3 3 4 ...

我尝试按照此页面上的示例进行操作:Secondary x-axis labels for sample size with ggplot2 on R

我的脚本现在看起来像这样:

K <- ggplot(DCE,aes(x=Level,y=Marginal))+
  geom_rect(data = DCE, aes(fill = hue),
            xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 0.3)+
  geom_point(colour = "black", size = 3)+
  geom_line(aes(group=Attribute), size=1, color = "black")+  ## connect subjects with a line
  #facet_grid(.~Attribute, scale="free")+        ## 1 row of panels by roost
  facet_grid(.~Attribute, scale="free", switch = "x")+        ## indien je de facet labels onder wilt
  theme_classic()+
  #theme(panel.background = element_rect(fill = 'lightgrey'))+
  zero_margin+                        ## squash together
  #theme(panel.border = element_rect(color = "black",
                                    #fill = NA,
                                    #size = 1))+
  theme(axis.text.x = element_text(angle = 90, hjust=0.95,vjust=0.2))+
  geom_errorbar(aes(ymin=marginLow, ymax=MarginHigh), width=.2, size=0.8,
                position=position_dodge(0.05))+
  theme(strip.background =element_rect(fill="white",colour = "white", size = 1))+
  theme(strip.text = element_text(colour = 'black', size = 10, face="bold"))+
  theme(strip.placement = "outside")+
  geom_hline(yintercept=0, 
             color = "#5E5E5E", size=0.5)+  # or #5E5E5E
  labs(
    x = "",
    y = "Average marginal effects ")+
  scale_fill_identity()+
  theme(axis.title.x = element_text(margin=margin(50,0,0,0))) +
  coord_cartesian(clip='off')+
  sub.div <- 1/length(DCE$Attribute)
for (i in 1:(length(DCE$Attribute)-1)) {
  K <- K + annotation_custom(
    linesGrob(
      x=unit(c(sub.div*i,sub.div*i), 'npc'),
      y=unit(c(0,-0.8), 'npc')   # length of the line below axis
    )
  )
}
K 

但这会导致混乱:

在此处输入图像描述

我就是想不通

标签: rggplot2

解决方案


推荐阅读