r - 删除 ggplot2 中的图例
问题描述
我做了这个情节:
我想删除右边的图例并将左边的图例一直移动到右边。
我试图让它与 一起工作theme(legend.position = "none")
,但没有成功。谁能帮我这个?
这是情节的代码
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
这是两个数据集:
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, 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.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")
这段代码:
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
")
这段代码:
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")
给
解决方案
一种选择是使用 cowplot 包(https://wilkelab.org/cowplot/index.html),特别是get_legend()
命令,例如
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)
您可以更改相对宽度以适应,例如
cowplot::plot_grid(p1_no_legend, p2, legend, nrow = 1,
rel_widths = c(1, 0.5, 0.3))
推荐阅读
- rust - Rust 中 Python zip 的最有效实现,用于未知长度的迭代器(Iterator
- >) 在编译时
- javascript - Safari 13.1.1 及更高版本的“beforeunload”功能未在浏览器窗口关闭时被调用
- javascript - 如何使用产品名称获取数组变量中的所有属性值?
- laravel-backpack - 如何在 laravel 背包中显示上传的文件?
- android - 有没有办法在 DatePicker 中隐藏最高年份选择器?
- javascript - Babel/Webpack 转译 JS 错误“对象不支持属性或方法‘最近的’”
- python-3.x - 当 CSV 内容读取为列表时,pandas read_csv() 返回具有 .1 和 .2 的重复条目
- reactjs - 如何跟踪在线串openlayer 6之间单击的鼠标
- reactjs - React 组件仅在刷新时加载
- php - Shopee API v1 add item error value unit 如何解决