r - Faceted density histogram -


i'm trying plot 2 histograms side-by-side, showing density of each observation value under each condition.

for example, if have following data frame:

> (test <- data.frame(rain=c(t,t,t,f,f), bikes=as.integer(c(1,1,2,1,2)), location=as.factor(c('a','b','a','b','b'))))    rain bikes location 1  true     1        2  true     1        b 3  true     2        4 false     1        b 5 false     2        b 

then want draw histogram rain=false 2 bars of height 0.5; , rain=true bars of height 1/3 , 2/3.

i've tried this

ggplot(test, aes(x=bikes, y=..density..)) +    geom_bar() +    scale_x_discrete() +    facet_wrap(~rain) +    scale_y_continuous(breaks=seq(0, 1, 0.05)) 

and gives correct shape, every bar 10% tall:

bad density picture

i've tried y=..count../sum(..count..), there bar heights 0.2, 0.2, 0.4, 0.2 - seems summing on whole data frame, not rain condition.

(i don't ..foo.. syntax. i've seen this answer, still don't density , count come from.)

i know create temporary data frame plot instead, prefer avoid - doing same data frame feels more flexible things might want in future - , haven't come non-awful way of doing it.

ideally, i'd color bars location. if ..density.., result:

enter image description here

where statistic's apparently been calculated on each of 4 conditions (rain-a, rain-b, dry-a, dry-b). want calculated on rain/dry condition.

heyo, it's easier modify data frame in order r want. using plyr package:

❥ library(plyr) ❥ test2 <- ddply(test, .(rain), transform, proportion = 1/length(rain)) ❥ test2    rain bikes location proportion 1 false     1        b     0.5000 2 false     2        b     0.5000 3  true     1            0.3333 4  true     1        b     0.3333 5  true     2            0.3333 ❥ ggplot(test2, aes(x=bikes)) + geom_bar(aes(y = proportion), stat= "identity") + facet_grid(~rain) + scale_y_continuous(labels=percent) + scale_x_continuous(breaks = 1:max(test2$bikes)) 

corrected plot


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -