3

Using this SO solution I created a facet with two "empty" plots, with the aim of combining with another group of facet_wrap plots, as shown below. The purpose is to have two y-axis labels for different unit measurements. How can I make the grid layout look like the top image, which produces the arrangement I want, but not the axis labels? This was accomplished with plot_grid with individual plots. My current output does not scale correctly and overlaps the other plots, as seen in the second image, but provides the axis labels. I have example data below, just copy and run the code to input it. enter image description here

enter image description here

library(ggplot2)
library(grid)
library(cowplot)

clipboard <- readClipboard()
test.data <- read.table(file = "clipboard", sep = ",", header=TRUE)
test.data1 <- test.data[1:24, ]
test.data2 <- test.data[25:32, ]

testplot1 <- ggplot(test.data1, aes(Station, value)) +
  geom_point() +
  labs(x = "Stations", y = "Scale A") +
  theme(legend.position = "none", legend.title = element_blank()) +
  facet_wrap( ~ constituent, ncol = 3, scales = "free_y")

testplot2 <- ggplot(test.data2, aes(Station, value)) +
  geom_point() +
  labs(x = "Stations", y = "Scale B") +
  theme(legend.position = "none", legend.title = element_blank(), axis.title.y = element_text(hjust = 0.2)) +
  facet_wrap( ~ constituent, ncol = 1, scales = "free_y")

blankplots <- ggplotGrob(testplot2)
rm_grobs <- blankplots$layout$name %in% c("panel-1-1", "panel-2-1", "strip-t-1-1", "strip-t-1-2")
blankplots$grobs[rm_grobs] <- NULL
blankplots$layout <- blankplots$layout[!rm_grobs, ]
grid.newpage()
emptygrids <- grid.draw(blankplots)

plot_grid(emptygrids, MPLOOplot1)

Example date is below:

Station,constituent,value
A1,A,1
B1,A,1
A1,B,2
B1,B,2
A1,C,3
B1,C,3
A1,D,4
B1,D,4
A1,E,5
B1,E,5
A1,F,6
B1,F,6
A1,G,7
B1,G,7
A1,H,8
B1,H,8
A1,I,9
B1,I,9
A1,J,10
B1,J,10
A1,K,11
B1,K,11
A1,L,1.4
B1,L,1.4
A1,Blank1,NA
B1,Blank1,NA
A1,Blank2,NA
B1,Blank2,NA
A1,XX,0.52
B1,XX,0.52
A1,YY,0.355
B1,YY,0.355
Anonymous coward
  • 2,061
  • 1
  • 16
  • 29

1 Answers1

4

I'm not sure I understand exactly what you're trying to do, so let me know if this is what you had in mind. I wasn't sure what you wanted colour to be mapped to, so I just used constituent for this example.

library(gridExtra)
library(ggplot2)
library(dplyr)
library(cowplot)
theme_set(theme_classic())

testplot1 <- ggplot(test.data1, aes(Station, value, colour=constituent)) +
  geom_point() +
  labs(x = "Stations", y = "Scale A") +
  theme(legend.title = element_blank()) +
  facet_wrap( ~ constituent, ncol = 3, scales = "free_y") +
  guides(colour=guide_legend(ncol=2))

testplot2 <- ggplot(test.data2 %>% filter(!grepl("Blank", constituent)), 
                    aes(Station, value, colour=constituent)) +
  geom_point() +
  labs(x = "Stations", y = "Scale B") +
  theme(legend.title = element_blank(), 
        axis.title.y = element_text(hjust = 0.2)) +
  facet_wrap( ~ constituent, ncol = 1, scales = "free_y")

leg1 = get_legend(testplot1)
leg2 = get_legend(testplot2)

testplot1 = testplot1 + guides(colour=FALSE)
testplot2 = testplot2 + guides(colour=FALSE)

Now we lay out the plots and legends with grid.arrange. This requires some manual tweaking of the heights and widths.

grid.arrange(
  arrangeGrob(
    arrangeGrob(nullGrob(), leg2, leg1, nullGrob(), ncol=4, widths=c(1,4,4,1)),
    testplot2, ncol=1, heights=c(4.2,5)
    ),
  testplot1, ncol=2, widths=c(1.1,3))

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • That is exactly what I am trying to do. Two separate y-axis titles, with a legend in the blank space. Thank you so much. I was trying to plot each group of plots as a facet_wrap, then combine, but the sizing wouldn't work, and the plot_grid would overlap, so manually working with the Grobs looks like the way to go. Thanks again, I've been trying many different things for two weeks. The color and shape will be factors that I left out for simplicity's sake. – Anonymous coward Mar 15 '18 at 19:05