2
votes

picture of plots

I have 3 plots, with two plots stacked on top of each other to appear as one plot. I made a side by side (2 column) plot so it looks like 2 plots next to each other (although again, the one plot is actually two plots stacked). I have two problems:

  1. No matter what I try (vjust, scale, etc) I can't get the bottom of the two side-by-side plots to be even.

  2. I can't get a Y axis label centered on the stacked plot, because you can't make a label that spans two plots. I've tried using "title" for the whole panel, but then I can't change the font size to match the rest of the labels.

I've tried using vjust, adding a title to the entire plot, using plot_grid instead, and everything else I can think of.

city<-c("A", "B", "A", "B", "A", "B")
temp<-c(20, 25, 300, 35, 30, 400)
data1<-data.frame(city, temp)

library(cowplot)
library(gridExtra)

g.top <- ggplot(data1[data1$variable == "city A", ], aes(x = temp)) 
+
geom_histogram(binwidth=1) +
scale_x_continuous(limits = c(0, 100), position = "top") +
scale_y_reverse(limits = c(400, 0))+
theme_minimal(base_size = 16) + theme(panel.grid.major = element_blank(), 
panel.grid.minor = element_blank(),
                      panel.background = element_blank(), axis.line = 
element_line(colour = "black"))+
annotate("text", x=20, y=200, cex=6, label= "city A")+ labs(title="a", 
y="Count")+theme(axis.title.x=element_blank())


g.bottom <- ggplot(data1[data1$variable == "city B", ], aes(x = 
temp)) +
geom_histogram(binwidth=1) +
scale_x_continuous(limits = c(0, 100)) + ylim(0, 400)+
theme_minimal(base_size = 16)+ theme(panel.grid.major = element_blank(), 
panel.grid.minor = element_blank(),
                      panel.background = element_blank(), axis.line = 
element_line(colour = "black"))+
annotate("text", x=20, y=200, cex=6, label= "city B") + labs(x = 
expression("Temp (F)"), y="") 

mean<-c(2, 6)
se<-c(0.01, 0.3)
city<-c("A","B")
df<-data.frame(mean, se, city)

gg<- ggplot(df, aes(x=city, y=mean, group=city, 
color=city, shape=city)) + 
geom_line(size=1, position=position_dodge(0.2))+
geom_jitter(aes(shape=city, size=city), alpha=1, 
position=position_dodge(0.2))+
geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.4, 
position=position_dodge(0.2))+
labs(title= "b", x= expression("City"), y =expression("Temp (F)"))+
theme_bw(base_size = 16) + theme(axis.title.x = 
element_text(vjust=200000000), axis.title.y = element_text(vjust=-7), 
legend.title=element_blank())+
theme(legend.justification=c(0.1,0.1), legend.position=c(0.6,0.1))+
scale_color_manual(values=c('#104E8BC8','#FF8C00C8'))+
scale_size_manual(values=c(4,4))+
scale_shape_manual(values=c(19, 17))+theme(legend.position="none")+
theme(axis.ticks.length=unit(-0.25, "cm"), axis.ticks.margin=unit(0.5, 
"cm"),axis.text.x = element_text(margin=unit(c(0.5,0.5,0.5,0.5), 
"cm")),axis.text.y = element_text(margin=unit(c(0.5,0.5,0.5,0.5), "cm")))

grid.arrange(arrangeGrob(g.top, g.bottom), gg, ncol = 2)
1
This doesn't help with your overall y axis label but I think the plot alignment looks pretty good right out of the box using package patchwork : (g.top/g.bottom)|gg. Patchwork isn't on CRAN yet; see github.com/thomasp85/patchwork - aosmith
Also see the code in the "Aligning complex graphics" section from the package egg overview here. I was able to adapt that to your situation and get good alignment (again not addressing the axis label issue). - aosmith

1 Answers

2
votes

You can use the patchwork package to lay out the plots. See the Vignette for details on how the layout system works:

To install package from github:

devtools::install_github("thomasp85/patchwork")

Then,

library(patchwork)

{g.top + g.bottom + plot_layout(ncol=1)} - gg

enter image description here