【问题标题】:按组自定义ggplot图例
【发布时间】:2022-01-23 05:46:07
【问题描述】:

我想创建一个如下图,但有一个按组的自定义图例。我有两个样本名称 A 和 B 以及关联的目标 (Y)。我想要的是将图例与一侧分组:
sample A:true vs obs
和其他方面
样本 B : true vs obs
具有相应的形状和颜色。 下面,这是我已经拥有的。

library(ggplot2)
sites = rep(seq(0.55,0.58,0.01), times = 4)
sites = sort(sites)

org = rep(c("Y_A", "Y_A_obs", "Y_b", "Y_b_obs"), times = 4)
values = rnorm(16)

df = data.frame(sites = sites, origine = org, values = values)

ggplot(df, aes(x=sites, y=values, color=origine)) + 
  geom_point(aes(shape=origine, color=origine), size=3)+
  scale_shape_manual(values=c(0, 15, 2, 17))+
  scale_color_manual(values=c("blue", "purple", "red", "orange"))

这就是我想要的:

感谢您的帮助!

【问题讨论】:

    标签: r ggplot2 grouping legend


    【解决方案1】:

    实现您想要的结果的一个选项是使用ggnewscale 包,它允许多个比例和图例用于相同的美学。这种方法的缺点是代码行数增加了一倍:

    library(ggplot2)
    library(ggnewscale)
    
    set.seed(123)
    
    sites = rep(seq(0.55,0.58,0.01), times = 4)
    sites = sort(sites)
    
    org = rep(c("Y_A", "Y_A_obs", "Y_b", "Y_b_obs"), times = 4)
    values = rnorm(16)
    
    df = data.frame(sites = sites, origine = org, values = values)
    
    ggplot() + 
      geom_point(data = subset(df, grepl("^Y_A", org)), aes(x=sites, y=values, shape=origine, color=origine), size=3)+
      scale_shape_manual(name = "A", values=c(Y_A = 0, Y_A_obs = 15), 
                         labels=c(Y_A = "y_true", Y_A_obs = "y_pred"), 
                         guide = guide_legend(order = 1))+
      scale_color_manual(name = "A", 
                         values=c(Y_A = "blue", Y_A_obs = "purple"), 
                         labels=c(Y_A = "y_true", Y_A_obs = "y_pred"), 
                         guide = guide_legend(order = 1)) +
      new_scale_color() +
      new_scale("shape") +
      geom_point(data = subset(df, grepl("^Y_b", org)), aes(x=sites, y=values, shape=origine, color=origine), size=3)+
      scale_shape_manual(name = "B", 
                         values=c(Y_b = 2, Y_b_obs = 17), 
                         labels=c(Y_b = "y_true", Y_b_obs = "y_pred"), 
                         guide = guide_legend(order = 2))+
      scale_color_manual(name = "B", 
                         values=c(Y_b = "red", Y_b_obs = "orange"), 
                         labels=c(Y_b = "y_true", Y_b_obs = "y_pred"),
                         guide = guide_legend(order = 2))
    

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2020-07-28
      • 2018-09-15
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多