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)
and this is what I want (the red lines with different tickness):
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:
I just can't figure it out