Forest plot in ggplot2

Hi there,

I'm trying to build a forest plot using ggplot2. Although I managed to generate a nice graph (code 1), the results are presented in two columns as there are two many groups and subgroups in it.
So, I'm trying to edit it, in order to make all results to fit in one single column (code 2). However, I'm struggling to reduce the spacing within the models in each group. Anyone has a clue how I can reduce the dimension of this graph and have all results presented in one single column?

Many thanks!!

code 1:
p=ggplot(data=data,
aes(x = Model2, y = hr, ymin = ll, ymax = ul ))+
geom_pointrange(aes(col=Model2))+
geom_hline(aes(yintercept =1, linetype='dashed'))+
xlab("")+
ylab("HR (95% Confidence Interval)")+
geom_errorbar(aes(ymin=ll, ymax=ul, col=Model2), width=0.2, cex=0.2)+
facet_wrap(Cancer~.,strip.position="left", nrow=27, scales = "free_y") +
theme(plot.title=element_text(size=8,face="bold"),
axis.text.y=element_text(hjust = 0.2, size = 4),
axis.text.x=element_text(hjust = 0.2, size = 4),
axis.ticks.y=element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
panel.background = element_blank(),
strip.text.y = element_text(size=8, hjust=0,vjust = 1,angle=90,face="bold"))+
theme_minimal()+
theme(legend.position = "none")+
print(ggtitle("Minimally processed food"))+
coord_flip()

ggsave (p,file='ggforest_N1.pdf', width = 8, height=15, dpi=300)

Code 2:
p=ggplot(data=data,
aes(x = Model2, y = hr, ymin = ll, ymax = ul, size=5 ))+
geom_pointrange(aes(col=Model2), size=0.2, shape=1)+
geom_hline(aes(yintercept =1, linetype='dotted'))+
xlab("")+
ylab("HR (95% Confidence Interval)")+
geom_errorbar(aes(ymin=ll, ymax=ul, col=Model2), width=0.2, cex=0.2)+
facet_wrap(Cancer~.,strip.position="left", nrow=27, scales = "free_y") +
theme_minimal()+
theme(plot.title=element_text(size=6,face="bold"),
axis.text.y=element_text(hjust = 0.2, lineheight = 0.2, size = 4),
axis.text.x=element_text(hjust = 0.2, size = 4),
panel.background = element_blank(),
panel.spacing.y = unit(0.5, "null"),
strip.text.y = element_text(size=4, hjust=0,vjust = 1,angle=90,face="bold"))+
theme(legend.position = "none")+
print(ggtitle("Minimally processed food"))+
coord_flip()

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Sorry for this - I'm new to this community and also to R (transitioning from Stata).

Here it go (please let me know if there is something missing...)

data.frame(
hr = c(0.64,0.89,0.9,0.54,0.8,0.82,0.62,
0.88,0.89,0.79,0.92,0.91,0.92,1.14,1.15,0.85,0.91,0.91,
0.81,0.98,0.99,1.2,1.37,1.36,0.7,0.89,0.92,0.95,0.99,
0.99,0.9,1.03,1.01,0.84,1.01,1.02,0.77,0.95,0.97,
0.87,0.95,0.96,0.93,1.01,1.01,1.12,0.97,0.98,1,0.99,
0.99,0.95,0.99,0.99,1.08,1.18,1.15,0.97,0.94,0.98,1.02,
1,1,1.01,0.99,0.98,1.02,1.03,1.03,0.86,0.96,0.97,
0.76,1.01,1.01,1.17,1.16,1.18,0.89,0.95,0.96,1.16,0.94,
0.93,0.95,1.01,1.03),
ll = c(0.6,0.81,0.82,0.49,0.69,0.71,0.57,
0.78,0.78,0.74,0.84,0.83,0.77,0.89,0.9,0.82,0.86,0.86,
0.78,0.91,0.92,0.97,1.03,1.01,0.64,0.78,0.81,0.84,
0.84,0.84,0.85,0.94,0.93,0.81,0.96,0.97,0.67,0.77,0.78,
0.83,0.89,0.89,0.9,0.97,0.97,0.91,0.75,0.75,0.91,0.88,
0.87,0.93,0.96,0.96,0.87,0.87,0.84,0.85,0.79,0.82,
0.96,0.92,0.92,0.93,0.9,0.89,1,1,1,0.8,0.87,0.88,
0.72,0.94,0.94,0.93,0.86,0.87,0.83,0.86,0.86,1.07,0.84,
0.83,0.9,0.94,0.94),
ul = c(0.69,0.98,1,0.59,0.93,0.96,0.68,1.01,
1.02,0.84,1.01,1,1.1,1.46,1.47,0.88,0.95,0.96,0.85,
1.04,1.06,1.49,1.84,1.83,0.77,1.01,1.04,1.07,1.16,
1.16,0.96,1.11,1.1,0.87,1.05,1.07,0.89,1.17,1.2,0.91,
1.02,1.03,0.96,1.06,1.06,1.38,1.25,1.27,1.1,1.12,1.12,
0.97,1.02,1.02,1.36,1.61,1.57,1.11,1.12,1.17,1.08,
1.09,1.08,1.08,1.09,1.08,1.05,1.07,1.07,0.91,1.06,1.06,
0.8,1.09,1.08,1.48,1.58,1.61,0.96,1.05,1.06,1.27,
1.04,1.04,1.01,1.1,1.11),
Cancer = as.factor(c("Oropharynx",
"Oropharynx","Oropharynx","Larynx","Larynx","Larynx",
"Esophagus","Esophagus","Esophagus","Stomach",
"Stomach","Stomach","Small intestine","Small intestine",
"Small intestine","Colon","Colon","Colon",
"Rectal","Rectal","Rectal","Anus","Anus","Anus",
"Liver","Liver","Liver","Gallbladder","Gallbladder",
"Gallbladder","Pancreas","Pancreas","Pancreas",
"Lung","Lung","Lung","Heart, Mediastinum and Pleura",
"Heart, Mediastinum and Pleura",
"Heart, Mediastinum and Pleura","Myeloma","Myeloma","Myeloma","Skin",
"Skin","Skin","Peritoneum","Peritoneum",
"Peritoneum","Soft Tissue","Soft Tissue","Soft Tissue",
"Breast","Breast","Breast","Vulva","Vulva","Vulva",
"Cervical","Cervical","Cervical","Endometrium",
"Endometrium","Endometrium","Ovary","Ovary",
"Ovary","Prostate","Prostate","Prostate","Kidney",
"Kidney","Kidney","Bladder","Bladder","Bladder",
"Eye","Eye","Eye","Brain","Brain","Brain",
"Thyroid","Thyroid","Thyroid","Lymph nodes","Lymph nodes",
"Lymph nodes")),
Model2 = as.factor(c("Model 1","Model 2",
"Model 3","Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2",
"Model 3","Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2","Model 3",
"Model 1","Model 2","Model 3","Model 1","Model 2",
"Model 3","Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2","Model 3",
"Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2","Model 3",
"Model 1","Model 2","Model 3","Model 1","Model 2",
"Model 3","Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2","Model 3",
"Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2","Model 3",
"Model 1","Model 2","Model 3","Model 1","Model 2",
"Model 3","Model 1","Model 2","Model 3","Model 1",
"Model 2","Model 3","Model 1","Model 2","Model 3",
"Model 1","Model 2","Model 3"))
)

install.packages("ggplot2")
library(ggplot2)

Code 1:
p=ggplot(data=data,
aes(x = Model2, y = hr, ymin = ll, ymax = ul ))+
geom_pointrange(aes(col=Model2))+
geom_hline(aes(yintercept =1, linetype='dashed'))+
xlab("")+
ylab("HR (95% Confidence Interval)")+
geom_errorbar(aes(ymin=ll, ymax=ul, col=Model2), width=0.2, cex=0.2)+
facet_wrap(Cancer~.,strip.position="left", nrow=27, scales = "free_y") +
theme(plot.title=element_text(size=8,face="bold"),
axis.text.y=element_text(hjust = 0.2, size = 4),
axis.text.x=element_text(hjust = 0.2, size = 4),
axis.ticks.y=element_blank(),
axis.title.y = element_blank(),
#axis.title.x = element_blank(),
panel.background = element_blank(),
strip.text.y = element_text(size=8, hjust=0,vjust = 1,angle=90,face="bold"))+
theme_minimal()+
theme(legend.position = "none")+
print(ggtitle("Minimally processed food"))+
coord_flip()

ggsave (p,file='ggforest_N1.pdf', width = 8, height=15, dpi=300)

code 2:
p=ggplot(data=data,
aes(x = Model2, y = hr, ymin = ll, ymax = ul, size=5 ))+
geom_pointrange(aes(col=Model2), size=0.2, shape=1)+
geom_hline(aes(yintercept =1, linetype='dotted'))+
xlab("")+
ylab("HR (95% Confidence Interval)")+
geom_errorbar(aes(ymin=ll, ymax=ul, col=Model2), width=0.2, cex=0.2)+
facet_wrap(Cancer~.,strip.position="left", nrow=27, scales = "free_y") +
theme_minimal()+
theme(plot.title=element_text(size=6,face="bold"),
axis.text.y=element_text(hjust = 0.2, lineheight = 0.2, size = 4),
axis.text.x=element_text(hjust = 0.2, size = 4),
panel.background = element_blank(),
panel.spacing.y = unit(0.5, "null"),
strip.text.y = element_text(size=4, hjust=0,vjust = 1,angle=90,face="bold"))+
theme(legend.position = "none")+
print(ggtitle("Minimally processed food"))+
coord_flip()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.