2

I have a set of coefficient estimates, and their standard errors from the R package BMA. I would like to produce a figure showing their value, with error bars. I've been able to make the plot, but have not been able to change the legend.

My code is:

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

Which produces this figure: enter image description here

I'd like the legend title to be "P!=0" rather than Psize (any guidance on changing != to not equals [\u2260] would be appreciated as well), and the labels to be "0-20", "20-40", "40-60", "60-80", "80-100" rather than 1,2,3,4,5.

Eventually, this will be plotted in a custom theme, based on theme_bw, to clean up the axes, etc. Any guidance would be appreciated!

Data:

   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
MAHE
  • 85
  • 6
  • [This](http://stackoverflow.com/a/5294214/324364) question should be enough to solve your first problem; don't be distracted by the focus on greek symbols. – joran Dec 31 '15 at 18:09
  • Since your legend is a size legend (and you map `aes(size = Psize)`), you need to use a `size` scale not a `fill` scale. (fill is for fill colors). If your `Psize` is a factor you can use `scale_size_discrete`. If it is a numeric, you will need to use `scale_size_continuous` (and probably specify breaks as well as labels). – Gregor Thomas Dec 31 '15 at 18:09
  • Oh, I didn't see your comment. Was busy faking the data... – Mike Wise Dec 31 '15 at 18:18
  • Feel bad now. Do you want to answer it? Seeing as your response beat my answer by several minutes at least. – Mike Wise Dec 31 '15 at 18:20
  • 1
    @MikeWise most of the time when I comment it's because I don't want to take the time to write a full answer (often because OP didn't share data). Don't feel bad - though my preference for this question is to close it as typo. – Gregor Thomas Dec 31 '15 at 18:21
  • Really? People can tear their hair out over this type of error (I used to), and they would never search for "typo". I know there are almost infinite variations of this kind of problem, but still... – Mike Wise Dec 31 '15 at 18:25
  • 1
    I also usually end up considering this sort of thing a typo, but [this question/answer](http://stackoverflow.com/questions/5171263/r-changing-line-colors-with-ggplot) could be considered a close duplicate in terms of needing to use the scale corresponding to the aesthetic. – aosmith Dec 31 '15 at 18:43
  • @joran Thanks for the link, very helpful. @Gregor and @Mike Wise, thanks for your explanations, that helps clear it up regarding `size` vs. `fill`. I agree with Mike Wise (probably because it was my question) that I wouldn't consider this a "typo" especially for those just getting into ggplot, as I am, and it's seemingly unending options! – MAHE Jan 01 '16 at 15:38

1 Answers1

4

You are just using the wrong attribute.

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

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • 1
    I will say, however, based on the legend and labels OP is getting, `Psize` is *probably* a factor in OP's data, so `scale_size_discrete` is appropriate. – Gregor Thomas Dec 31 '15 at 18:22
  • Thanks! `scale_size_continuous` works well, though `scale_size_discrete` does not, I assume because the 'structure' of the data has `Psize` as `numeric`. `Psize` is actually a factor for displaying `P` in bins of 20 points. – MAHE Jan 01 '16 at 15:29