1

I am struggling to add a modified legend to my ggplot.

I have a data set:

birdspp smallsaplings   mediumsaplings  largesaplings
95      5.044642857     2.384615385     1.30952381
97      3.482269504     1.873684211     1.390625
63      6.285714286     2               2.4
57      5.216216216     1.666666667     1.125

Which I've created a graph with:

Graph

Using the code:

 library(ggplot2)
 library(dplyr)
 library(tidyr)
    
 sapling %>% 
   gather(sapling_type, mean_number, -birdspp)
      
 sapling %>% 
     gather(sapling_type, mean_number, -birdspp) %>% 
     ggplot(aes(mean_number, birdspp)) + 
       geom_point(aes(color = sapling_type)) + 
       geom_smooth(aes(group =sapling_type, color = sapling_type, fill = sapling_type), method = "lm", show.legend=FALSE) + 
       labs(x="Mean Sapling Density", y="Number of  Bird Species") + 
       theme_classic() 

As you can see the legend is not very neat.

I've tried turning show.legend = FALSE on and adding this:

+ scale_colour_discrete(name  ="Sapling Size",
                            breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
                            labels=c("smallsaplings", "mediumsaplings", "largesaplings")) +
      scale_shape_discrete(name  ="Sapling Size",
                            breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
                            labels=c("smallsaplings", "mediumsaplings", "largesaplings"))

But haven't had any luck. Any suggestions would be appreciated.

Thank you.

Community
  • 1
  • 1
  • What exactly does the "legend is not very neat" mean? What's the desired output? – MrFlick Mar 10 '17 at 15:34
  • As in 'smallsaplings' has no space between the words in the key. Same problem with the others too. Ideally I'd like the title removed as well. – JazTheBilloligist Mar 10 '17 at 16:02
  • It would be easier to make specific suggestion if you provided a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data. But you should leave off the `show.legend=FALSE` but keep the `scale_color_discrete` but change the values in `labels=` to the names with spaces. – MrFlick Mar 10 '17 at 16:08
  • The input data is at the top there? Apologies I'm a bit of a newbie with this. I had already tried that and get the error: Error in +scale_colour_discrete(name = "Sapling Size", breaks = c("smallsaplings", : invalid argument to unary operator – JazTheBilloligist Mar 10 '17 at 16:44
  • Sorry, somehow I missed the data earlier. My answer should work correctly. – MrFlick Mar 10 '17 at 16:47
  • No problem. Thank you that works perfect. – JazTheBilloligist Mar 10 '17 at 16:54

1 Answers1

1

This should work

 sapling %>% 
     gather(sapling_type, mean_number, -birdspp) %>% 
     ggplot(aes(mean_number, birdspp)) + 
       geom_point(aes(color = sapling_type)) + 
       geom_smooth(aes(group =sapling_type, color = sapling_type, fill = sapling_type), method = "lm", show.legend=FALSE) + 
       labs(x="Mean Sapling Density", y="Number of  Bird Species") + 
       theme_classic() +
       scale_colour_discrete(name="",
           breaks=c("smallsaplings", "mediumsaplings", "largesaplings"),
           labels=c("Small Saplings", "Medium Saplings", "Large Saplings"))

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295