r - How to get summary for each panel in facet_wrap() in ggplot? -


this generic question, based on facet_wraps() in ggplot see in link below ex: ggplot hadley/staticdocs

is there way summary() of data each panel of facet_wrap or supposed write function sort of information ?

for example lets consider plot above link:

p + geom_point(data = transform(cyl6, cyl = null), colour = "red") + facet_wrap(~ cyl) 

how % of red vs black dots each panel ?

i recommend combination of approaches above. notably, helpful prespecify colors want ahead of time make easier control outputs , summary statistics.

for example, specify colors according rule (the code have above doesn't seem work):

my_mpg <- transform(mpg, dot_color = ifelse(hwy > 20, 'red','black')) 

then plot data , specify colors manually:

ggplot(my_mpg,aes(x=displ,hwy)) + geom_point(aes(color=dot_color)) +         facet_wrap(~cyl) + scale_color_manual(values=c('black','red')) 

for aggregating statistics think data.table provides best interface:

library(data.table) my_mpg <- as.data.table(my_mpg) my_mpg[,list(percent_black = sum(dot_color=='black') / .n),by=cyl]  ##    cyl percent_black ## 1:   4    0.04938272 ## 2:   6    0.32911392 ## 3:   8    0.84285714 ## 4:   5    0.00000000 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -