user2502904
user2502904

Reputation: 319

Color fill manual and x axis

This R code was adapted from here - cookbook-r:

Score density plots for White and Green separately

library(ggplot2)

set.seed(1234)
dat <- data.frame(cond = factor(rep(c("White","Green"), each=1000000)), 
                  Score = c(rnorm(1000000),rnorm(1000000, mean=0)))
dat$Score <- ifelse(dat$cond == "Green", dat$Score - 1, dat$Score)
by(dat$Score, dat$cond,  mean) 

# Density plots with semi-transparent fill
ggplot(dat, aes(x=Score, fill=cond)) + geom_density(alpha=.3)

The code runs without syntax errors. I have two issues with the code:

  1. I cannot determine how to change the colors. The White condition is displayed as green-ish and and the Green condition is displayed as orange-ish. I would like the White condition to be white-ish and the Green condition to be green-ish.

  2. The X-axis has tick marks at -6, -3, 0, and 3. I would like tick marks at -6, -5, -4, -3, -2, -1, 0, 1, 2, 3.

Upvotes: 0

Views: 526

Answers (2)

Tobias Hotzenplotz
Tobias Hotzenplotz

Reputation: 1217

ggplot(dat, aes(x=Score)) + 
  geom_density(aes(fill=cond), alpha=.3) +
  scale_fill_manual(values = c("Green" = "darkseagreen", 
                               "White" = "antiquewhite1")) +
  scale_x_continuous(breaks = -6:3)

you can use the "Colorpicker" R Studio Addin to find pretty R colours (install.packages("colourpicker"), then Tools > Addins...)

Upvotes: 0

markus
markus

Reputation: 26373

ggplot(dat, aes(x=Score, fill=cond)) + geom_density(alpha=.3) +
scale_fill_manual(values = c("Green" = "green", "White" = "white")) +
scale_x_continuous(breaks = seq(-6, 3))

Upvotes: 2

Related Questions