I am trying to generate a patchwork figure composed of 3 different ggplot objects. Each has to have the same x axis. I am trying to do two things: 1) Collect the legends 2) Show the middle plot's y axis label only and the bottom plot's x axis label only. When I use
plot_layout(guides = "collect")
it only collects two - still giving me two legends.
I am not really sure how to do the second one.
The code I am using is:
# Plot each as an object, then combine in the end
#Full
MEAN_full <- ggplot(full_50, mapping = aes(x = tau, y = mean, group = t))+
geom_path(aes(colour = t))+
labs(x = bquote(tau*" (s)"),
y = bquote(gamma*"ln"(tau)(~Omega~ "cm"^2)),
colour = "Time after \nplating (h)",
title = "Cu-LFP cell 16 \n 0.5 mAh/cm2")+
scale_colour_gradient(low = 'blue', high = 'red')+
theme_classic(base_size = 20)+
scale_x_continuous(limits = c(10^-6, 10^0),
trans = 'log10',
breaks=trans_breaks('log10', function(x) 10^x),
labels=trans_format('log10', math_format(10^.x)))+
ylim(0,50)
MEAN_full
#ANode
MEAN_an <- ggplot(anode_50, mapping = aes(x = tau, y = mean, group = t))+
geom_path(aes(colour = t))+
labs(x = bquote(tau*" (s)"),
y = bquote(gamma*"ln"(tau)(~Omega~ "cm"^2)),
colour = "Time after \nplating (h)",
title = "Cu-LFP cell 16 \n 0.5 mAh/cm2")+
scale_colour_gradient(low = 'blue', high = 'red')+
theme_classic(base_size = 20)+
scale_x_continuous(limits = c(10^-6, 10^0),
trans = 'log10',
breaks=trans_breaks('log10', function(x) 10^x),
labels=trans_format('log10', math_format(10^.x)))+
ylim(0,75)
MEAN_an
#Cathode
MEAN_cath <- ggplot(cath_50, mapping = aes(x = tau, y = mean, group = t))+
geom_path(aes(colour = t))+
labs(x = bquote(tau*" (s)"),
y = bquote(gamma*"ln"(tau)(~Omega~ "cm"^2)),
colour = "Time after \nplating (h)",
title = "Cu-LFP cell 16 \n 0.5 mAh/cm2")+
scale_colour_gradient(low = 'blue', high = 'red')+
theme_classic(base_size = 20)+
scale_x_continuous(limits = c(10^-6, 10^0),
trans = 'log10',
breaks=trans_breaks('log10', function(x) 10^x),
labels=trans_format('log10', math_format(10^.x)))+
ylim(0,50)
MEAN_cath
comb <- MEAN_an/MEAN_cath/MEAN_full
comb
comb <- comb + plot_layout(guides = 'collect')
comb
Each plot contains a lot of data, but the data for between plots is of exactly the same format - same length and same variables. I'm happy to email anybody data for reproducible examples. Without plot_layout():enter image description here
With plot_layout():enter image description here
Without a reproducible example it's hard to be sure, but I would guess that the colour scale limits are different for two of the graphs. Consider the following example:
# Set up two data frames to plot
df1 <- data.frame(x=1:10,y=runif(10))
df2 <- data.frame(x=1:10,y=runif(10,1,2))
Graph without fixing the limits
(ggplot(df1, aes(x,y,color=y)) + geom_point() + scale_color_gradient()+
(ggplot(df2, aes(x,y,color=y)) + geom_point()+ scale_color_gradient()+
plot_layout(guides="collect")
After fixing the limits:
(ggplot(df1, aes(x,y,color=y)) + geom_point() + scale_color_gradient(limits=c(0,2)))+
(ggplot(df2, aes(x,y,color=y)) + geom_point()+ scale_color_gradient(limits=c(0,2)))+
plot_layout(guides="collect")
Although the best way to solve both your problems (the scale and the axes) is to combine the data into one big frame and use facets. Here you'll only see the 'outermost' axes, which is what I think you want in your graph.
dplyr::bind_rows("Group 1"=df1,"Group 2"=df2,.id = "group") |>
ggplot(aes(x,y,color=y)) + geom_point() + facet_wrap(~group)