4

I am a beginner and reading Wickham's ggplot2 book to understand how this thing works. I am a little confused about setting attributes (color, size etc.) inside and outside aes() I have included references I've referred here so far.

I ran XYZ experiments to understand this:

Data:

df <- data.frame(x = c(1,2,3), y = c(1,2,3), z=c("a","b","c"))

Experiment 1:

 ggplot(df, aes(y, y)) +
   geom_point(size = 4, show.legend = TRUE) 

This doesn't show the legend although I've set it to TRUE. I am not sure why this is happening.

Experiment 2:

ggplot(df, aes(y, y)) +
   geom_point(size = 4, aes(colour = z)) #I see the legend

This surprisingly shows the legend. I believe that from references, mapping variables to "color" is a valid reason to show the legend. We have mapped a variable to color.

Experiment 3

ggplot(df, aes(y, y)) +
   geom_point(size = 4, aes(colour = z),color = "red") 

Here, I believe red color is overridden by my second call i.e. color="red". Am I correct?

Experiment 4:

ggplot(df, aes(y, y)) +
   geom_point(size = 4, aes(colour = z,size=20),color = "red") 

I wanted to test whether the size increases if I set size = 20 inside aes. It doesn't. I am not sure why because size = 4 was set before setting size = 20.

Experiment 5:

ggplot(df, aes(y, y)) +
   geom_point(size = 4, color = "red") +
   geom_point(aes(colour = z),size=10) #legend appears, but I have lost border colors. 

Finally, I thought of keeping size = 10 outside of aes because I thought setting size = constant inside aes would be defaulting the value of size. This worked in that the size did increase, but I have lost the legend.

I am completely lost -how do I show the legend, increase the size (using variables or manually) or change colors( using variables or manually)? I understand that changing size and colors would be the easiest because I would just have to map them to variables. However, I am not sure when I would want to keep one of them (say size or color) constant.

 ggplot(df, aes(y, y)) +
   geom_point(aes(colour = z, size = x)) ##this maps color and size to variables.

I'd appreciate any thoughts. Finally, I am sorry if my question is too basic for some of you.


References: Difference between passing options in aes() and outside of it in ggplot2


Update from the thread: (after discussions for those of us who are reading this...)

Experiment 1: No legend shows up because there is no need for one. There is no variable is mapped to size. There's nothing to show in the legend in that case.

Experiment 2: There is one layer and it maps to the variable. So, I see the legend.

Experiment 3: I have set the color using variable, but then it's overridden by red color.

Experiment 4 Ditto here. The output of Expt 4 and 3 are the same because of overriding issue.

Experiment 5 The second layer overlays on top of first layer. One way to reverse this would be:

ggplot(df, aes(y, y)) + geom_point(aes(colour = z),size=10) +
   geom_point(size = 4, color = "red")
Community
  • 1
  • 1
watchtower
  • 4,140
  • 14
  • 50
  • 92

1 Answers1

2

You should read more on defining aesthetics globally, and defining them on local geom_xyz() layers.

The thing is, when you define col as attributes, it overrides your aesthetics (aes()) mapping. You can see that in your Experiment 3.

In the fourth one, you are again overriding the color by mapping it outside of aes().

You should check this link out on aesthetics mapping: http://docs.ggplot2.org/current/aes.html

From the same link:

Aesthetics supplied to ggplot() are used as defaults for every layer

you can override them, or supply different aesthetics for each layer

It's nothing so complicated, you should look at the documentation more carefully and it'll come to you naturally.

Pj_
  • 824
  • 6
  • 15
  • Ok. here's what I think is going on: **Experiment 2:** There is one layer and it maps to the variable. So, I see the legend. **Experiment 3:** I have set the color using variable, but then it's overridden by red color. **Experiment 4** Ditto here. The output of Expt 4 and 3 are the same because of overriding issue. **Experiment 5** The second layer overlays on top of first layer. One way to reverse this would be `ggplot(df, aes(y, y)) + geom_point(aes(colour = z),size=10) + geom_point(size = 4, color = "red")` However, I am not sure why legend is not shown in Experiment 1. Any thoughts? – watchtower Aug 25 '16 at 20:52
  • 2
    No legend in experiment 1 because no variables are mapped to colour, fill, size, etc. You've only mapped to x and y (`aes(y,y)` is equivalent to `aes(x=y, y=y)`), which won't produce a legend. – eipi10 Aug 25 '16 at 20:55
  • @ss0208535 In the Experiment 1, you haven't mapped anything inside the global aesthetics, which is why there's no legend. Add 'color = z' inside the aes() of ggplot and you'll see the legend. – Pj_ Aug 25 '16 at 20:56
  • @ss0208535 Did that helped? :) – Pj_ Aug 25 '16 at 21:11
  • @eipi10 I see what's the source of my confusion...Excel would have shown a legend for a `y->y` mapping. I am coming from Excel world, so I get confused sometimes. If I understand you correctly--to show the legend, I would have to map a variable to the aesthetic; setting it (such as `size =2`) to a constant won't be sufficient to produce the legend although it would change the aesthetic. Am I correct? – watchtower Aug 25 '16 at 21:19
  • @Pj_ Thanks for taking time to help me. That does help me to some extent... – watchtower Aug 25 '16 at 21:23
  • @ss0208535 To product a legend, you need to map at least some variable to the `aes()`. – Pj_ Aug 25 '16 at 21:25
  • 2
    Why would you want a legend for `size` if no variable is mapped to size? There's nothing to show in the legend in that case. If you mean you want to map a variable to color, but also set the size of points, then just do `geom_point(size = 4, aes(colour = z))`. This will give you a legend showing what the colors mean, and all the points will have size=4. – eipi10 Aug 25 '16 at 21:28
  • @eipi10 That makes sense. Thanks eipi10. This makes sense. Thank you so much for your help. – watchtower Aug 25 '16 at 21:51
  • @ss0208535 If the discussion has helped you, can you accept it? It might help others who face issue similar to yours. :) – Pj_ Aug 25 '16 at 21:53
  • @Pj_ Great point. I have updated the question with the discussion with eipi10 and yourself. Thanks so much, for your help... – watchtower Aug 25 '16 at 21:56