首页 > 解决方案 > 删除 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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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")

这段代码:

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")

在此处输入图像描述

标签: rggplot2

解决方案


一种选择是使用 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)

示例_1.png

您可以更改相对宽度以适应,例如

cowplot::plot_grid(p1_no_legend, p2, legend, nrow = 1,
                   rel_widths = c(1, 0.5, 0.3))

示例_2.png


推荐阅读