【问题标题】:Cannot change legend in ggplot2 [closed]无法更改ggplot2中的图例[关闭]
【发布时间】:2015-12-31 18:03:30
【问题描述】:

我有一组系数估计值,以及它们来自 R 包BMA 的标准误差。我想制作一个带有误差线的数字来显示它们的价值。我已经能够制作情节,但无法改变传说。

我的代码是:

ggplot(res.BMA[-c(1,8),], aes(x = Beta, y = EV)) +
  geom_errorbar(aes(ymin = EV-SD, ymax = EV+SD), width = 0.1) +
  geom_point(aes(size = Psize)) +
  facet_wrap(~Year) +
  xlab("Parameter") +
  ylab("Estimated Value") +
  scale_fill_discrete(name = "P!=0", labels = c("0-20", "20-40", "40-60", "60-80", ">80")) 

这会产生这个数字:

我希望图例标题为“P!=0”而不是 Psize(任何关于将 != 更改为不等于 [\u2260] 的指导也将不胜感激),标签为“0-20” ", "20-40", "40-60", "60-80", "80-100" 而不是 1,2,3,4,5。

最终,这将绘制在基于theme_bw 的自定义主题中,以清理轴等。任何指导将不胜感激!

数据:

   Year                             Beta     P Psize        EV      SD
1  1978                        Intercept 100.0     5 -2.278802 0.10678
2  1978         Homestead Density (250m)  89.1     5  0.245324 0.13542
3  1978        Structure Density (1000m)  17.8     1 -0.009627 0.06958
4  1978             %Edge forest (250m)  38.2     2  0.078142 0.14320
5  1978            %Core forest (2000m)  50.6     3 -0.116838 0.15194
6  1978                        Mean NDVI  16.1     1 -0.006095 0.04509
7  1978 Homestead Density × %Edge forest  24.0     2 -0.027491 0.17412
8  2001                        Intercept 100.0     5 -2.096206 0.10638
9  2001         Homestead Density (250m)  40.2     3  0.067039 0.10918
10 2001        Structure Density (1000m)  57.1     3  0.204915 0.21991
11 2001             %Edge forest (250m)  31.5     2  0.044161 0.09353
12 2001            %Core forest (2000m)  57.7     3 -0.208547 0.22165
13 2001                        Mean NDVI  17.6     1 -0.001539 0.04867
14 2001 Homestead Density × %Edge forest  19.3     1  0.008105 0.07373

【问题讨论】:

  • This question 应该足以解决您的第一个问题;不要因为对希腊符号的关注而分心。
  • 由于您的图例是尺寸图例(并且您映射aes(size = Psize)),您需要使用size 比例而不是fill 比例。 (填充用于填充颜色)。如果您的Psize 是一个因素,您可以使用scale_size_discrete。如果它是一个数字,您将需要使用scale_size_continuous(并且可能指定中断和标签)。
  • 哦,我没有看到你的评论。忙着伪造数据……
  • @MikeWise 大多数时候我发表评论是因为我不想花时间写一个完整的答案(通常是因为 OP 没有共享数据)。不要感到难过 - 尽管我更喜欢这个问题是因为错字而关闭它。
  • 我通常也会认为这种事情是一个错字,但this question/answer 在需要使用与美学相对应的比例方面可能被认为是一个近似的重复。

标签: r plot ggplot2 legend


【解决方案1】:

你只是使用了错误的属性。

n <- 14
set.seed(1234)
df <- data.frame(Year=sample(c("1978","2001"),n,replace=T),Beta<-1:n,
                  Psize=sample(1:5,n,replace=T),EV=rnorm(n,2),SD=rnorm(n,0.1,0.1))
ggplot(df, aes(x = Beta, y = EV)) +
  geom_errorbar(aes(ymin = EV-SD, ymax = EV+SD), width = 0.1) +
  geom_point(aes(size = Psize)) +
  facet_wrap(~Year) +
  xlab("Parameter") +
  ylab("Estimated Value") +
  scale_size_continuous(name = "P!=0", 
                  labels = c("0-20", "20-40", "40-60", "60-80", ">80")) 

【讨论】:

  • 我会说,但是,根据 OP 获得的图例和标签,Psize可能是 OP 数据中的一个因素,因此scale_size_discrete 是合适的。
  • 谢谢! scale_size_continuous 效果很好,虽然scale_size_discrete 没有,但我认为是因为数据的“结构”有PsizenumericPsize 实际上是在 20 个点的 bin 中显示 P 的一个因素。
猜你喜欢
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2015-07-11
  • 2013-03-06
  • 2021-01-02
  • 2020-04-10
相关资源
最近更新 更多