Reputation: 623
I have made this plot:
I would like to delete the legend on the right and move the legend on the left all the way to the right.
I have tried to make it work with theme(legend.position = "none")
, however unsuccessfully. Can anyone help me with this?
Here's the code for the plot
library(ggplot2)
library(ggpmisc)
p1 <- ggplot(df1, aes(carbon,
water,
fill=soil_type)) +
geom_smooth(method = "lm",
formula = formula,
color="black") +
geom_point(aes(shape=soil_type,
color=soil_type,
size=soil_type)) +
scale_fill_manual(values=c("green3", "brown")) +
scale_color_manual(values=c("black", "black")) +
scale_shape_manual(values=c(21, 24))+
scale_size_manual(values=c(2.7, 2.0))+
labs(shape="soil_type",
color="soil_type") +
theme_bw() +
facet_wrap(~days,
ncol = 2)+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label),
stat(p.value.label),
sep = "*\", \"*")),
formula = formula,
rr.digits = 2,
p.digits = 1,
parse = TRUE,size=3.5)
p2 <- ggplot(df2, aes(x=days,
y=slope,
fill=soil_type))+
geom_point(aes(shape=soil_type,
color=soil_type,
size=soil_type)) +
scale_fill_manual(values=c("green3", "brown")) +
scale_shape_manual(values=c(21, 24))+
scale_color_manual(values=c("black", "black")) +
scale_size_manual(values=c(2.7, 2.0))+
labs(shape="soil_type",
color="soil_type") +
theme_bw()
p2 + theme(legend.position = "none")
p1 + p2
And here are the two datasets:
df2<-structure(list(days = c(0L, 0L, 4L, 4L, 10L, 10L, 17L, 17L, 24L,
24L, 66L, 66L, 81L, 81L, 94L, 94L, 116L, 116L), soil_type = c("mineral",
"organic", "mineral", "organic", "mineral", "organic", "mineral",
"organic", "mineral", "organic", "mineral", "organic", "mineral",
"organic", "mineral", "organic", "mineral", "organic"), slope = c(-0.8,
-0.18, -1.48, -0.29, -1.6, -0.19, -0.89, 0.01, -0.68, 0, -0.01,
0, 0, 0, -0.06, 0, -0.06, 0)), row.names = c(NA, -18L), class = c("tbl_df",
"tbl", "data.frame"))
df1<-structure(list(carbon = c(1.4, 0.8, 1.6, 0.1, 0.4, 0.4, 0.4,
1.3, 0.4, 1.1, 0.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, 1.3, 1.8, 1.3, 1.8, 1.8, 1.9, 1.7, 1.4, 1, 1.3,
1, 1.9, 1.7, 1.2, 1, 1, 1.7, 0.9, 1, 1.7, 1.9, 1.4, 1.3, 1.5,
1.6, 1.3, 1.5, 1.6, 1.9, 1.4, 1.7, 2, 1.2, 2, 1.8, 1.7, 1.5,
2), soil_type = c("organic", "mineral", "organic", "mineral",
"mineral", "mineral", "mineral", "organic", "mineral", "organic",
"mineral", "mineral", "mineral", "mineral", "mineral", "mineral",
"mineral", "mineral", "mineral", "mineral", "organic", "mineral",
"mineral", "mineral", "mineral", "mineral", "mineral", "mineral",
"organic", "mineral", "mineral", "organic", "mineral", "mineral",
"mineral", "mineral", "organic", "mineral", "mineral", "organic",
"mineral", "organic", "mineral", "mineral", "mineral", "organic",
"mineral", "mineral", "organic", "mineral", "organic", "mineral",
"mineral", "mineral", "mineral", "organic", "mineral", "mineral",
"mineral", "mineral", "mineral", "mineral", "mineral", "mineral",
"mineral", "mineral", "organic", "mineral", "mineral", "mineral",
"mineral", "mineral", "mineral", "mineral", "organic", "mineral",
"mineral", "organic", "mineral", "mineral", "mineral", "mineral",
"organic", "mineral", "mineral", "organic", "mineral", "organic",
"mineral", "organic", "mineral", "mineral")), row.names = c(NA,
-92L), class = "data.frame")
This code:
y_axis <- ggplot(data.frame(l = acetone_c_vs_cc$labels$y, x = 1, y = 1)) +
geom_text(aes(x, y, label = l), angle = 90) +
theme_void() +
coord_cartesian(clip = "off")
acetone_c_vs_cc$labels$y <- acetone_c_vs_wc$labels$y <- acetone_c_vs_cn$labels$y <- " "
y_axis + acetone_c_vs_cc + acetone_c_c_slope_org_min + acetone_c_vs_wc + acetone_c_wc_slope_org_min + acetone_c_vs_cn +
acetone_c_wc_slope_org_min +
plot_layout(widths = c(1, 25, 5),
guides = "collect",
design = "
123
145
167
")
This code:
y_axis <- ggplot(data.frame(l = acetone_c_vs_cc$labels$y, x = 1, y = 1)) +
geom_text(aes(x, y, label = l), angle = 90) +
theme_void() +
coord_cartesian(clip = "off")
acetone_c_vs_cc$labels$y <- acetone_c_vs_wc$labels$y <- acetone_c_vs_cn$labels$y <- " "
ggarrange(y_axis, acetone_c_vs_cc, acetone_c_c_slope_org_min, acetone_c_vs_wc, acetone_c_wwc_slope_org_min, acetone_c_vs_cn,
acetone_c_wwc_slope_org_min,
plot_layout(widths = c(1, 25, 5),
design = "
123
145
167
"),
common.legend = TRUE, legend = "right")
gives
Upvotes: 0
Views: 200
Reputation: 35
use the plot_layout() in patchword package.
(p1 + p2) + plot_layout(guides = 'collect')
You may find this post useful: https://fahimahmad.netlify.app/posts/combining-multiple-plots-using-patchwork/#annotation
Upvotes: 0
Reputation: 26705
One option is to use the cowplot package (https://wilkelab.org/cowplot/index.html), specifically the get_legend()
command, e.g.
library(tidyverse)
library(ggpmisc)
#install.packages("cowplot")
library(cowplot)
df2<-structure(list(days = c(0L, 0L, 4L, 4L, 10L, 10L, 17L, 17L, 24L,
24L, 66L, 66L, 81L, 81L, 94L, 94L, 116L, 116L), soil_type = c("mineral",
"organic", "mineral", "organic", "mineral", "organic", "mineral",
"organic", "mineral", "organic", "mineral", "organic", "mineral",
"organic", "mineral", "organic", "mineral", "organic"), slope = c(-0.8,
-0.18, -1.48, -0.29, -1.6, -0.19, -0.89, 0.01, -0.68, 0, -0.01,
0, 0, 0, -0.06, 0, -0.06, 0)), row.names = c(NA, -18L), class = c("tbl_df",
"tbl", "data.frame"))
df1<-structure(list(carbon = c(1.4, 0.8, 1.6, 0.1, 0.4, 0.4, 0.4,
1.3, 0.4, 1.1, 0.2, 1, 0.4, 0.4, 0.5, 0.8, 0.1, 0.5, 0.4, 0.6,
1.1, 0.6, 0.2, 0.2, 0.4, 0.1, 0.3, 0.5, 1.4, 0.3, 0.3, 1.1, 0.3,
0.7, 0.4, 0.4, 1.1, 0.1, 0.6, 1.3, 0.1, 1.6, 0.4, 0.5, 0.5, 1.2,
0.5, 0.5, 1.4, 0.8, 1.6, 0.1, 0.4, 0.4, 0.4, 1.1, 0.2, 1, 0.4,
0.4, 0.5, 0.8, 0.1, 0.5, 0.4, 0.6, 1.1, 0.6, 0.2, 0.2, 0.4, 0.1,
0.3, 0.5, 1.4, 0.3, 0.3, 1.1, 0.3, 0.7, 0.4, 0.4, 1.1, 0.1, 0.6,
1.3, 0.1, 1.6, 0.4, 1.2, 0.5, 0.5), days = c(0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 94L, 94L, 94L,
94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L,
94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L,
94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L, 94L,
94L, 94L), water = c(2, 1.6, 1.9, 1.2, 1.3, 1.5, 1.7, 1.9, 1.3,
1.8, 1.3, 1.8, 1.8, 1.9, 1.7, 1.4, 1, 1.3, 1, 1.9, 1.7, 1.2,
1, 1, 1.7, 0.9, 1, 1.7, 1.9, 1.4, 1.3, 1.5, 1.6, 1.3, 1.5, 1.6,
1.9, 1.4, 1.7, 2, 1.2, 2, 1.8, 2, 1.8, 1.7, 1.5, 2, 2, 1.6, 1.9,
1.2, 1.3, 1.5, 1.3, 1.8, 1.3, 1.8, 1.8, 1.9, 1.7, 1.4, 1, 1.3,
1, 1.9, 1.7, 1.2, 1, 1, 1.7, 0.9, 1, 1.7, 1.9, 1.4, 1.3, 1.5,
1.6, 1.3, 1.5, 1.6, 1.9, 1.4, 1.7, 2, 1.2, 2, 1.8, 1.7, 1.5,
2), soil_type = c("organic", "mineral", "organic", "mineral",
"mineral", "mineral", "mineral", "organic", "mineral", "organic",
"mineral", "mineral", "mineral", "mineral", "mineral", "mineral",
"mineral", "mineral", "mineral", "mineral", "organic", "mineral",
"mineral", "mineral", "mineral", "mineral", "mineral", "mineral",
"organic", "mineral", "mineral", "organic", "mineral", "mineral",
"mineral", "mineral", "organic", "mineral", "mineral", "organic",
"mineral", "organic", "mineral", "mineral", "mineral", "organic",
"mineral", "mineral", "organic", "mineral", "organic", "mineral",
"mineral", "mineral", "mineral", "organic", "mineral", "mineral",
"mineral", "mineral", "mineral", "mineral", "mineral", "mineral",
"mineral", "mineral", "organic", "mineral", "mineral", "mineral",
"mineral", "mineral", "mineral", "mineral", "organic", "mineral",
"mineral", "organic", "mineral", "mineral", "mineral", "mineral",
"organic", "mineral", "mineral", "organic", "mineral", "organic",
"mineral", "organic", "mineral", "mineral")), row.names = c(NA,
-92L), class = "data.frame")
##
p1 <- ggplot(df1, aes(carbon,
water,
fill=soil_type)) +
geom_smooth(method = "lm",
formula = formula,
color="black") +
geom_point(aes(shape=soil_type,
color=soil_type,
size=soil_type)) +
scale_fill_manual(values=c("green3", "brown")) +
scale_color_manual(values=c("black", "black")) +
scale_shape_manual(values=c(21, 24))+
scale_size_manual(values=c(2.7, 2.0))+
labs(shape="soil_type",
color="soil_type") +
theme_bw() +
facet_wrap(~days,
ncol = 2)+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label),
stat(p.value.label),
sep = "*\", \"*")),
formula = formula,
rr.digits = 2,
p.digits = 1,
parse = TRUE,size=3.5)
p1_no_legend <- ggplot(df1, aes(carbon,
water,
fill=soil_type)) +
geom_smooth(method = "lm",
formula = formula,
color="black") +
geom_point(aes(shape=soil_type,
color=soil_type,
size=soil_type)) +
scale_fill_manual(values=c("green3", "brown")) +
scale_color_manual(values=c("black", "black")) +
scale_shape_manual(values=c(21, 24))+
scale_size_manual(values=c(2.7, 2.0))+
labs(shape="soil_type",
color="soil_type") +
theme_bw() +
theme(legend.position = "none") +
facet_wrap(~days,
ncol = 2)+
stat_poly_eq(
aes(label = paste(stat(adj.rr.label),
stat(p.value.label),
sep = "*\", \"*")),
formula = formula,
rr.digits = 2,
p.digits = 1,
parse = TRUE,size=3.5)
p2 <- ggplot(df2, aes(x=days,
y=slope,
fill=soil_type))+
geom_point(aes(shape=soil_type,
color=soil_type,
size=soil_type)) +
scale_fill_manual(values=c("green3", "brown")) +
scale_shape_manual(values=c(21, 24))+
scale_color_manual(values=c("black", "black")) +
scale_size_manual(values=c(2.7, 2.0))+
labs(shape="soil_type",
color="soil_type") +
theme_bw() +
theme(legend.position = "none")
legend <- cowplot::get_legend(p1)
cowplot::plot_grid(p1_no_legend, p2, legend, nrow = 1)
And you can change the relative widths to suit, e.g.
cowplot::plot_grid(p1_no_legend, p2, legend, nrow = 1,
rel_widths = c(1, 0.5, 0.3))
Upvotes: 1