I would like to draw exactly (or something similar to) this following figure A:
I used this following code. But definitely would need some help to customize my figure.
Any help is welcome.
Thanks in advance
groupe2<-rep(c(rep("P",4),rep("I",4)),3)
groupe<-rep(c("PPP","PPI","PII","PIP","III","IIP","IPP","IPI"),3)
OR_A<-c(1.00,0.86,0.90,0.88,0.70,0.77,0.77,0.68)
ICinf_A<-c(NA,0.70,0.76,0.72,0.61,0.61,0.60,0.50)
ICsup_A<-c(NA,1.06,1.06,1.07,0.81,0.96,1.00,0.92)
OR_B<-c(1.00,0.97,0.81,1.01,0.58,0.61,0.73,0.69)
ICinf_B<-c(NA,0.78,0.62,0.77,0.52,0.50,0.61,0.57)
ICsup_B<-c(NA,1.20,1.05,1.28,0.65,0.71,0.81,0.82)
OR_C<-c(1.00,1.03,0.65,0.86,0.37,0.47,0.68,0.58)
ICinf_C<-c(NA,0.84,0.50,0.67,0.33,0.40,0.59,0.49)
ICsup_C<-c(NA,1.27,0.86,1.10,0.41,0.56,0.78,0.69)
outcome<-c(rep("PC M",8), rep("RIC M",8), rep("RIC C",8))
OR<-c(OR_A,OR_B,OR_C)
ICinf<-c(ICinf_A,ICinf_B,ICinf_C)
ICsup<-c(ICsup_A,ICsup_B,ICsup_C)
dataOR<-data.frame(OR,groupe,outcome,groupe2,ICinf,ICsup)
#pour mettre l'ordre qu'on veut pour la légende (par défaut : ordre alphabétique)
dataOR[, "groupe"] <- factor(dataOR[, "groupe"] ,
levels = c("PPP","PPI","PII","PIP","III","IIP","IPP","IPI"))
##########
##########
ggplot(dataOR, aes(fill=outcome, y=OR, x=groupe)) +
geom_bar(position="dodge", stat="identity") +
scale_fill_brewer(palette="Blues")+
geom_errorbar(aes(ymin=ICinf, ymax=ICsup), width=.2,position=position_dodge(.9))+
#theme(panel.background = element_rect(fill="lightgreen"))+
geom_hline(yintercept=1)
Regarding the labels for figure A:
For example:
PPP, etc...
would be at the place of
UPDATE UPDATEBasic Activity Level, etc...
It looks like you want to use the facetting functionality of ggplot
. Some slight modifications to your code gives you the facets, removes the x-axis, and adds the points:
ggplot(dataOR, aes(fill=outcome, y=OR, x=factor(1))) + #Reset x-axis
geom_bar(position="dodge", stat="identity") +
scale_fill_brewer(palette="Blues")+
geom_errorbar(aes(ymin=ICinf, ymax=ICsup), width=.2,position=position_dodge(.9))+
geom_point(position=position_dodge(.9)) + #Add points
geom_hline(yintercept=1) +
facet_wrap(~groupe, nrow = 1) + #Add facets
scale_x_discrete(name = NULL, labels = NULL, breaks = NULL) #Remove labels
Per the comment below, to change the colors of the specific groups you can do:
colors = c(brewer.pal(3, "Blues"), rep(brewer.pal(3,"Reds"), 3), rep(brewer.pal(3, "Blues"), 4))
ggplot(dataOR, aes(fill=interaction(outcome, groupe), y=OR, x=factor(1))) +
geom_bar(position="dodge", stat="identity") +
scale_fill_manual(values = colors, guide = FALSE) +
geom_errorbar(aes(ymin=ICinf, ymax=ICsup), width=.2,position=position_dodge(.9))+
geom_point(position=position_dodge(.9)) +
geom_hline(yintercept=1) +
facet_wrap(~groupe, nrow = 1) +
scale_x_discrete(name = NULL, labels = NULL, breaks = NULL)
I think I caught everything that you wanted from the first plot. I did a few tweaks, some with theme
parameters and some a little hacky:
geom_point
but set it to not show up in the legend I think that's everything that differed between the two!
ggplot(dataOR, aes(fill=outcome, y=OR, x=groupe)) +
geom_bar(position="dodge", stat="identity", color = "gray95", size = 0.25) +
# scale_fill_brewer(palette="Blues")+
scale_fill_manual(values = RColorBrewer::brewer.pal(5, "Blues")[3:5]) +
geom_errorbar(aes(ymin=ICinf, ymax=ICsup), width=.4, position=position_dodge(.9))+
geom_hline(yintercept=1) +
geom_point(position = position_dodge(0.9), size = 0.5, show.legend = F) +
scale_y_continuous(expand = expand_scale(mult = c(0, 0.05))) +
facet_wrap(~groupe, nrow = 1, scales = "free_x") +
labs(fill = NULL) +
theme(legend.position = "top",
legend.key.height = unit(0.2, "cm"),
legend.background = element_rect(color = "black", size = 0.4),
axis.line = element_line(color = "black"),
axis.text.x = element_blank(),
axis.ticks = element_blank(),
panel.grid.major.x = element_blank(),
axis.title = element_text(face = "bold"))
#> Warning: Removed 3 rows containing missing values (geom_errorbar).
Created on 2018-05-04 by the reprex package (v0.2.0).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.