Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill in ggplot2 is always red, regardless of what colour is actually specified. #1675

Closed
a-hurst opened this issue Jul 21, 2016 · 7 comments
Closed

Comments

@a-hurst
Copy link

a-hurst commented Jul 21, 2016

For whatever reason, whenever I try to specify a fill colour for a violin plot it always renders as a pastel red, regardless of what I tell it. Here is some simple code that reproduces the bug for me in a fresh R session:

library(ggplot2)

blah <- rnorm(2000, 3, 1.5)
blah2 <- rep("test", times=2000)
df = data.frame(a = blah2, b = blah)
ggplot(data = df) + geom_violin(aes(x = a, y = b, fill="grey80"))

Running this code gives me this:

screen shot 2016-07-21 at 11 21 27 am

This happens no matter which fill colour I specify. When I don't specify a fill, the fill defaults to white as expected:

screen shot 2016-07-21 at 11 23 31 am

This happens with the official CRAN binary in the R GUI.app as well as with R 3.3.1 installed from Homebrew. I also did a fresh install of ggplot2 on a fresh install of R when I generated the above graphs. Any ideas what's going on?

Platform info:

R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
@jac0bean9
Copy link

You need fill to be outside aes, i.e..:

`library(ggplot2)

blah <- rnorm(2000, 3, 1.5)
blah2 <- rep("test", times=2000)
df = data.frame(a = blah2, b = blah)
ggplot(data = df) + geom_violin(fill="grey80", aes(x = a, y = b))`

image

@a-hurst
Copy link
Author

a-hurst commented Jul 21, 2016

Thanks, it works properly now! Any reason it defaults to that red instead of throwing an error or something if it's defined within aes()? Had it thrown an error I would have been much less bewildered.

@steveharoz
Copy link
Contributor

By putting the values in aes, you're basically generating a new factor variable and a legend.

ggplot(mtcars)+
    geom_point(aes(x = mpg, y = wt, color = 'blah'))+
    geom_point(aes(x = mpg, y = drat, color = 'asdfasdf'))+
    scale_color_manual(values = c('blah' = 'blue', 'asdfasdf' = 'red'))

image
Notice the legend.

If you set the values outside of aes, ggplot2 won't make the legend for you.

ggplot(mtcars)+
  geom_point(aes(x = mpg, y = wt), color = 'blue')+
  geom_point(aes(x = mpg, y = drat), color = 'red')

image

@a-hurst
Copy link
Author

a-hurst commented Jul 21, 2016

Ah, that makes perfect sense. Thanks again for your help!

@a-hurst a-hurst closed this as completed Jul 21, 2016
@ipstone
Copy link

ipstone commented Mar 22, 2018

Might I ask why it makes sense? I am new to ggplot, using qplot, somehow my fill color is always red. what is wrong with qplot(x, y, color='blue'...), why is it always 'red'?

@has2k1
Copy link
Contributor

has2k1 commented Mar 23, 2018

@ipstone, if you are new to ggplot2 stay away from qplot.

Might I ask why it makes sense?

The aesthetic values in aes() are data which is properly fit to a scale, hence it must be interpreted with a guide/legend. Aesthetic values outside aes() are direct settings which by-pass the scaling mechanism hence no need for a guide/legend.

@lock
Copy link

lock bot commented Sep 19, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Sep 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants