5

Suppose I have following dataset:

set.seed(seed=10)
n <- 10000
s.data <- data.frame(score = rnorm(n,500,100),
                     gender = sample(c("Male","Female"),size=n,replace=T,prob=c(.4,.6)),
                     major = sample(c("A","B","C","D"),size=n,replace=T,prob=c(.02,.25,.05,.68)))

I create following histogram:

require(ggplot2)
ggplot(s.data, aes(x=score)) + facet_wrap(~ major) +
  geom_histogram(binwidth=50,colour="black", fill="white") 

Histogram 1

Since I want more details about major A and C, I plot a density histogram:

ggplot(s.data, aes(x=score)) + facet_wrap(~ major) +
  geom_histogram(binwidth=50,aes(y = ..density..),colour="black", fill="white") 

Histogram 2 Perfect till this point.

When I try to do same thing with a categorical variable (instead of a continuous one), I can able to do frequencies but cannot able to plot density:

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

enter image description here

As I wanted.

But I flunk with this graph:

ggplot(s.data, aes(gender)) + 
       geom_histogram(aes(y = ..density..),colour="black", fill="white") + 
       facet_wrap(~ major)

enter image description here

Any ideas? Thanks in advance.

HBat
  • 4,873
  • 4
  • 39
  • 56

0 Answers0