I'm trying to make a figure with three panels in ggplot. I would like to remove axis title and text wherever two of the panels adjoin (see example below) and position one legend in the empty facet.
Panel A and B should have an identical y-axis and all three panels have identical x-axis (A and B are basically plots of the full data set and a subset whereas C shows the percentage change). I saw many helpful posts here on stackoverflow, but none explained exactly what I would like to know.
I tried:
1) plot the panels as facets with facet_wrap()
, use element_blank()
to control axis limits (as explained here) and use this function to replace the legend:
# function to replace legend
library(lemon)
shift_legend2 <- function(p) {
# ...
# to grob
gp <- ggplotGrob(p)
facet.panels <- grep("^panel", gp[["layout"]][["name"]])
empty.facet.panels <- sapply(facet.panels, function(i) "zeroGrob" %in% class(gp[["grobs"]][[i]]))
empty.facet.panels <- facet.panels[empty.facet.panels]
# establish name of empty panels
empty.facet.panels <- gp[["layout"]][empty.facet.panels, ]
names <- empty.facet.panels$name
# example of names:
#[1] "panel-3-2" "panel-3-3"
# now we just need a simple call to reposition the legend
reposition_legend(p, 'center', panel=names)
}
# add dummy data for element_blank()
dat <- dat %>%
mutate(y_min = ifelse(figure == "pct_change", 0.8, 0),
y_max = ifelse(figure == "pct_change", 1.6, 150))
# plot
plot1 <- ggplot(dat, aes(x = year, y = values)) +
geom_line(aes(col = class)) +
theme_cowplot(12) +
facet_wrap(~figure, nrow = 2, scales = "free_y") +
geom_blank(aes(y = y_min)) +
geom_blank(aes(y = y_max))
# rearrange legend
shift_legend2(plot1)
2) plot panels seperately and arrange later
a <- ggplot(dat[dat$figure == "d_complete",], aes(x = year, y = values)) +
geom_line(aes(col = class)) +
theme_cowplot(12) +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.text.x = element_blank())
b <- ggplot(dat[dat$figure == "d_subset",], aes(x = year, y = values)) +
geom_line(aes(col = class)) +
theme_cowplot(12) +
theme(legend.position = "none",
axis.title.y = element_blank(),
axis.text.y = element_blank()) +
ylim(c(0, 120)
c <- ggplot(dat[dat$figure == "pct_change",], aes(x = year, y = values)) +
geom_line(aes(col = class)) +
theme_cowplot(12) +
theme(legend.position = "none")
plot2 <- plot_grid(a, b, c, ncol=2, align = "hv", axis = "bl")
Now the problems are:
- in plot 1 the x-axis-line of panel A disappears, I cannot put different y-axis legends for panel A and C and the y-axis text of panel B is still there
- in plot 2 there are very ugly gaps between the plots and I don't know how to add the legend
Thanks for your help!
Data:
dput(dat)
structure(list(class = c("AUN", "AUN", "AUN", "AUN", "AUN", "AUN",
"AUN", "AUN", "AUN", "CHE", "CHE", "CHE", "CHE", "CHE", "CHE",
"CHE", "CHE", "CHE"), year = c(0L, 0L, 0L, 5L, 5L, 5L, 10L, 10L,
10L, 0L, 0L, 0L, 5L, 5L, 5L, 10L, 10L, 10L), figure = c("d_subset",
"d_complete", "pct_change", "d_subset", "d_complete", "pct_change",
"d_subset", "d_complete", "pct_change", "d_subset", "d_complete",
"pct_change", "d_subset", "d_complete", "pct_change", "d_subset",
"d_complete", "pct_change"), values = c(73, 99, 1, 84, 118, 1.19191919191919,
80, 96, 0.96969696969697, 12, 16, 1, 19, 27, 1.6875, 16, 20,
1.25), y_min = c(0, 0, 0.8, 0, 0, 0.8, 0, 0, 0.8, 0, 0, 0.8,
0, 0, 0.8, 0, 0, 0.8), y_max = c(150, 150, 1.6, 150, 150, 1.6,
150, 150, 1.6, 150, 150, 1.6, 150, 150, 1.6, 150, 150, 1.6)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -18L), groups = structure(list(
class = c("AUN", "CHE"), .rows = list(1:9, 10:18)), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))