Reputation: 1692
I have a figure that includes four plots. All y-axis are Temperature and the two x-axis are Time1 and Time2. I am stacking and aligning all four plots into a 2x2 figure using the patchwork
package and plot_layout()
function in R. Since, all y-axes are the same variable, I only want to show one y-axis title on the left side of the plot, but I can't figured out how to do this. I also want to cover-up the y-axes on the two plots in the right column, but when I move the figures, the right column overlaps the left column (I want the opposite).
Here is an example of what I have done so far.
library(ggplot2)
library(patchwork)
df1 <- data.frame(x1 = rnorm(20), y1 = rnorm(20))
df2 <- data.frame(x2 = rnorm(20), y2 = rnorm(20))
df3 <- data.frame(x3 = rnorm(20), y3 = rnorm(20))
df4 <- data.frame(x4 = rnorm(20), y4 = rnorm(20))
p1 <- ggplot(data = df1, aes(x = x1, y = y1)) +
geom_point() +
ylab("Temperature") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.margin = unit(c(0,0,0,0),"cm"))
p2 <- ggplot(data = df2, aes(x = x2, y = y2)) +
geom_point() +
ylab("Temperature") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.margin = unit(c(0,0,0,0),"cm"))
p3 <- ggplot(data = df3, aes(x = x3, y = y3)) +
geom_point() +
xlab("Time1") +
ylab("Temperature") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.margin = unit(c(-0.5,0,0,0),"cm"))
p4 <- ggplot(data = df4, aes(x = x4, y = y4)) +
geom_point() +
xlab("Time2") +
ylab("Temperature") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.margin = unit(c(-0.5,0,0,0),"cm"))
p5 <- p1/p2/p3/p4 + plot_layout(ncol = 2, heights = c(1,1))
p5
However, I would ultimately like the figure to look something like this:
Upvotes: 0
Views: 607
Reputation: 24818
How about using a facet_grid
? We'll need to do some data cleanup first though:
library(dplyr)
library(tidyr)
libary(ggplot2)
df1 <- data.frame(Sample = 1, Time1 = rnorm(20), y = rnorm(20))
df2 <- data.frame(Sample = 1, Time2 = rnorm(20), y = rnorm(20))
df3 <- data.frame(Sample = 2, Time1 = rnorm(20), y = rnorm(20))
df4 <- data.frame(Sample = 2, Time2 = rnorm(20), y = rnorm(20))
data <- bind_rows(mget(ls(pattern = "df[0-9]"))) %>%
pivot_longer(cols = c(-y,-Sample),
names_to = "Time",
values_to = "x") %>%
dplyr::filter(complete.cases(.))
data
## A tibble: 80 x 4
# Sample y Time x
# <dbl> <dbl> <chr> <dbl>
# 1 1 -0.449 Time1 0.768
# 2 1 -1.10 Time1 0.410
# 3 1 -0.529 Time1 -1.98
# 4 1 -0.485 Time1 -1.09
# 5 1 0.128 Time1 1.06
## … with 75 more rows
Now that we have the data in the correct form, we can facet_wrap
by Time:
ggplot(data = data, aes(x = x, y = y)) +
geom_point() +
ylab("Temperature") +
xlab(element_blank()) +
facet_grid(cols = vars(Time), rows = vars(Sample), scales = "free",switch = "both") +
theme_bw() +
theme(panel.grid = element_blank(),
plot.margin = unit(c(0,0,0,0),"cm"),
strip.background = element_blank(),
strip.placement = "outside", strip.text.y = element_blank())
Note that there is no way to have a free y-axis within the same row nor free x-axis within the same column.
Upvotes: 2