Reputation: 125
I realized that ggplot assigns the colours to the vertical lines in a different way than I expect it to do.
Creating the data frame:
wages <- rnorm(100, 0.9, 0.5)
ids <- as.factor(round(rnorm(100, 1342, 98)))
x <- data.frame(ids, wages)
Drawing the distribution, specifying colours of the vertical lines:
ggplot(x, aes(x = wages)) +
geom_density(alpha=.4, colour = "darkgrey", fill = "darkgrey") +
geom_vline(data = x, aes(xintercept = mean(x$wages), colour = "green"),
linetype = 1, size = 0.5)+
geom_vline(data = x, aes(xintercept = median(x$wages), colour = "blue"),
linetype = 1, size = 0.5) +
geom_vline(data = x, aes(xintercept = mean(x$wages)+1*sd(x$wages), colour = "red"),
linetype = 1, size = 0.5)+
xlim(c(0,3))
Your graph will look differently, however one result stays the same: The median and the mean+1sd line colours are switched. Does anyone know how to fix this? Thanks
Upvotes: 0
Views: 194
Reputation: 98589
As you are providing colors by their name and not mapping to variable, colour=
should be placed outside the aes()
.
ggplot(x, aes(x = wages)) +
geom_density(alpha=.4, colour = "darkgrey", fill = "darkgrey") +
geom_vline(data = x, aes(xintercept = mean(x$wages)),colour = "green",
linetype = 1, size = 0.5)+
geom_vline(data = x, aes(xintercept = median(x$wages)),colour = "blue",
linetype = 1, size = 0.5) +
geom_vline(data = x, aes(xintercept = mean(x$wages)+1*sd(x$wages)),colour = "red",
linetype = 1, size = 0.5)+
xlim(c(0,3))
Upvotes: 2