Align the double line chart and the line chart on the x axis when both charts have the same X axis. Ggplot2

I tried to do this for quite some time and so far I have not been able to align two different types of diagrams along the X axis, which are the same. I just need a double line chart on top of the histogram when both charts have the same X axis.

Below is my details below, along with the code I have written so far, and several attempts.

My details:

Date <- c("2015-07-22", "2015-07-23", "2015-07-24", "2015-07-25", "2015-07-26", "2015-07-27", "2015-07-28", "2015-07-29", "2015-07-30", "2015-07-31", "2015-08-01", "2015-08-02", "2015-08-03", "2015-08-04", "2015-08-05") Mean_temp12z <- c(66.6, 64.6, 67.6, 69.6, 72.0, 71.8, 73.0, 72.2, 72.8, 71.8, 69.4, 64.2, 61.2, 62.0, 63.4) Mean_temp0z <- c(62.8, 65.0, 67.4, 71.6, 72.4, 71.6, 71.0, 71.8, 71.6, 69.6, 69.2, 66.2, 60.6, 63.6, 66.4) temp_deltalow <- c( 3.8, -0.4, 0.2, -2.0, -0.4, 0.2, 2.0, 0.4, 1.2, 2.2, 0.2, -2.0, 0.6, -1.6, -3.0) GFS_low_changes <- data.frame(Date, Mean_temp12z, Mean_temp0z, temp_deltalow) 

What I have drawn so far is a two-line graph shown using this code and the image below:

 low_line <- ggplot(GFS_low_changes, aes(Date))+ geom_line(aes(y= Mean_temp12z, colour= "Mean_temp12z" ))+ geom_line(aes(y= Mean_temp0z, colour= "Mean_temp0z"))+ geom_point(aes(y= Mean_temp12z))+ geom_point(aes(y= Mean_temp0z))+ theme(legend.title= element_text(colour="black", size=12))+ scale_color_discrete(name="GFS Models")+ labs(y= "Temperature °F") 

two-line chart

I also built a diagram showing the delta between model 0z and model 12z, shown with the code and image below:

 low_bar <- ggplot(data = GFS_low_changes, aes(x= Date, y = temp_deltalow)) + geom_bar(colour= "black", fill= ifelse(temp_deltalow>0,"red","blue"), stat= "identity", position = "identity") + geom_text(aes(label= paste(temp_deltalow, "°F"),hjust= 0.4, vjust= ifelse(temp_deltalow>0,-0.5,1)), size= 5) 

bar plot

So, when creating both of these charts, I would like to combine them along the X axis with a line chart on top of the histogram. The mutual distance between the two is preferable, but I could not get close to them.

I tried using the grid.draw function from the gridExtra package with the code:

  grid.newpage() grid.draw(rbind(ggplotGrob(low_line), ggplotGrob(low_bar), recording= T)) 

but I get the error message: Error: ncol (x) == ncol (y) is not TRUE

I also used grid.arrange , which gives me better results, but not where close to the same x axis and seamless integration between the two diagrams.

I want to avoid working here, but I tried to melt the data frame above and was only able to build graphic strings. But any help in this area is also greatly appreciated.

I basically ran away from this example during my attempts: https://gist.github.com/tomhopper/faa24797bb44addeba79

Any help on this is greatly appreciated!

+8
r ggplot2 gtable
source share
2 answers

This will align the x axis and correct the layout. However, an interval still exists between the figures.

 ## Specify the xlimits xlims <- as.Date(c("2015-07-21", "2015-08-06")) ## low_bar as you had it, except the ylimits are adjusted for labels and xlims added low_bar <- ggplot(data = GFS_low_changes, aes(x= Date, y = temp_deltalow)) + geom_bar(colour= "black", fill= ifelse(temp_deltalow>0,"red","blue"), stat= "identity", position = "identity") + geom_text(aes(label= paste(temp_deltalow, "°F"),hjust= 0.4, vjust= ifelse(temp_deltalow>0,-0.5,1)), size= 5) + ylim(-4, 5) + xlim(xlims) ## low_line, legend is changed and xlims is added low_line <- ggplot(GFS_low_changes, aes(Date)) + geom_line(aes(y= Mean_temp12z, colour= "Mean_temp12z" )) + geom_line(aes(y= Mean_temp0z, colour= "Mean_temp0z"))+ geom_point(aes(y= Mean_temp12z))+ geom_point(aes(y= Mean_temp0z))+ theme(legend.title= element_text(colour="black", size=12))+ scale_color_discrete(name="GFS Models")+ labs(y= "Temperature °F") + theme(legend.position = c(0.9,0.8)) + xlim(xlims) ## Construct the layout, 2 rows one column grid.newpage() pushViewport(viewport(layout = grid.layout(2, 1))) print(low_bar, vp = viewport(layout.pos.row = 2, layout.pos.col=1)) print(low_line, vp = viewport(layout.pos.row = 1, layout.pos.col=1)) 

enter image description here

+3
source share

you can use combine() from gridExtra (modified version of gtable :: join),

 library(gridExtra) g1 <- ggplotGrob(low_line) g2 <- ggplotGrob(low_bar) colnames(g1) <- paste0(seq_len(ncol(g1))) colnames(g2) <- paste0(seq_len(ncol(g2))) grid.draw(combine(g1, g2, along=2)) 

the same result can be obtained using gtable , but it is less convenient, since i) join has errors (dev version is fixed), ii) rbind does not like to compare units with unit.pmax . This is why I now duplicated these functions in gridExtra.

enter image description here

+10
source share

All Articles