0

I have the following plot: I want to place lines between de x-axis labels (see second plot for what I want in red). I also want to make the background of the first facet grey and second white and so forth.

this is my code so far:

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)

this is my plot: enter image description here

and this is what I want (the red lines with different tickness): enter image description here

this is str() of my dataset:

> 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 ...

I tried following the example on this page: Secondary x-axis labels for sample size with ggplot2 on R

My script looks like this now:

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 

But that results in this mess:

enter image description here

I just can't figure it out

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Karima21
  • 51
  • 1
  • 9
  • Could you make your example reproducible, e.g. with a built-in datadaset? I don't think its possible with vanilla ggplot2, but people can be creative sometimes. – teunbrand Oct 06 '21 at 12:49
  • @teunbrand I have added str() of my dataset, is this what you mean? – Karima21 Oct 06 '21 at 12:58
  • No, I meant something I can paste in my R console and have your code run with that data, typically the output of `dput(data_sample)`. See also https://stackoverflow.com/a/5963610/11374827. But we don't exactly need your data, we just need some data and code that can serve as a platform from which to start solving the problem. – teunbrand Oct 06 '21 at 13:10
  • Oh so what do you need from me now to help me? @teunbrand – Karima21 Oct 06 '21 at 13:27
  • @teunbrand, I think the solution is in this answer: However I just can't follow what they have done. Could you help me with it? https://stackoverflow.com/questions/61549243/secondary-x-axis-labels-for-sample-size-with-ggplot2-on-r – Karima21 Oct 07 '21 at 08:18

0 Answers0