gb <- read.csv('results-gradient-boosting.csv')
p <- ggplot(gb) + geom_point(aes(x = pred, y = y),alpha = 0.4, fill = 'darkgrey', size = 2) +
geom_line(aes(x = pred, y = pred,color = 'darkgrey'),size = 0.6) +
geom_line(aes(x = pred, y = pred + 3,color = I("darkgrey")), linetype = 'dashed',size = 0.6) +
geom_line(aes(x = pred, y = pred -3,color = 'darkgrey'),linetype = 'dashed',size = 0.6)
My code is above. I have no idea why when I put color inside aes, the color turns out to be red. But if I put it outside of aes, it is correct. Thanks for your help!
When you put color="darkgrey"
outside aes
, ggplot takes it literally to mean that the line should be colored "darkgrey"
. But when you put color="darkgrey"
inside aes
, ggplot takes it to mean that you want to map color to a variable. In this case, the variable has only one value: "darkgrey". But that's not the color "darkgrey"
. It's just a string. You could call it anything. The color ggplot chooses will be based on the default palette. Map color to a variable when you want different colors for different levels of that variable.
For example, see what happens in the example below. The colors are chosen from ggplot's default palette and are completely independent of the names we've used for colour
in each call to geom_line
. You will get the same three colors when you have any color aesthetic that takes on three different unique values:
library(ggplot2)
theme_set(theme_classic())
ggplot(mtcars) +
geom_line(aes(mpg, wt, colour="green")) +
geom_line(aes(mpg, wt - 1, colour="blue")) +
geom_line(aes(mpg, wt + 1, colour="star trek"))
But now we put the colors outside aes
so they are taken literally, and we comment out the third line, because it will cause an error if we don't use a valid colour.
ggplot(mtcars) +
geom_line(aes(mpg, wt), colour="green") +
geom_line(aes(mpg, wt - 1), colour="blue") #+
#geom_line(aes(mpg, wt + 1), colour="star trek")
Note that if we map colour to an actual column of mtcars
(one that has three unique levels), we get the same three colors as in the first example, but now they are mapped to an actual feature of the data:
ggplot(mtcars) +
geom_line(aes(mpg, wt, colour=factor(cyl)))
And finally, what if we want to set those mapped colors to different values:
ggplot(mtcars) +
geom_line(aes(mpg, wt, colour=factor(cyl))) +
scale_colour_manual(values=c("purple", hcl(150,100,80), rgb(0.9,0.5,0.3)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With