ss0208535 - 1 year ago 80
R Question

# Setting colors and size inside aes() and inside geom() in ggplot2

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

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