HBat
HBat

Reputation: 5702

Coloring density plot in ggplot2

When I use following code to generate a density plot:

require(ggplot2)
set.seed(seed=10)
n <- 10000
s.data <- data.frame(score = rnorm(n,500,100),
                     gender = sample(c("Male","Female","No Response"),size=n,replace=T,prob=c(.4,.55,.05)),
                     major = sample(c("A","B","C","D"),size=n,replace=T,prob=c(.02,.25,.05,.68))) 
ggplot(s.data, aes(major,..density..,fill=major,group=1)) + 
  geom_histogram() + facet_wrap(~ gender)

enter image description here

I cannot distinguish between categories of "major" by color.

What I want to get is density plot similar to this frequency plot in the sense of colors and legend:

ggplot(s.data, aes(major,fill=major)) + 
    geom_histogram() + facet_wrap(~ gender)

enter image description here

This question is following my question (here) which is already answered here.

Upvotes: 2

Views: 616

Answers (1)

topchef
topchef

Reputation: 19823

You can still try frequency plot with facet parameter scale="free_y":

ggplot(s.data, aes(major,..count..,fill=major)) + 
  geom_histogram() + facet_wrap(~ gender, scale="free_y")

enter image description here

Upvotes: 1

Related Questions