无法在ggplot2中更改图例

时间:2015-12-31 18:03:31

标签: r plot ggplot2 legend

我有一组系数估计值,以及来自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")) 

产生这个数字: enter image description here

我希望传说中的标题是" P!= 0"而不是Psize(任何关于改变的指导!=不等于[\u2260]也将被赞赏),并且标签是" 0-20"," 20-40&#34 ;," 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

1 个答案:

答案 0 :(得分:3)

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

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

enter image description here