Alternative similar to geom_density

777 views
Skip to first unread message

Nick

unread,
Jun 21, 2013, 4:11:57 PM6/21/13
to ggp...@googlegroups.com
Hello,

I recently created a plot using geom_density and it is really informative about the topic I am studying. However, some of my collaborators worry about the use of density on the y-axis rather than say frequency or raw count because density t is not something that is usually reported in my field. I have spent some time trying to develop a plot using other geom's such as, geom_bar, geom_freqpoly, and geom_area, but really haven't come up with something as nice as the geom_density plot. Nice as in I really like how smooth the overlapping distributions look with geom_density. The others look clunky or jagged. Does anyone have any suggestions on how to make a plot similar to geom_density, but instead of density on the y axis have frequency instead?

Below is some example code to get you started.


date <- abs(round(c(rnorm(n=100,mean=5,sd=1),rnorm(n=50,mean=15,sd=1))))
assign.type <- c(rep("Yes",times=100), rep("No",times=50))
tmp <- data.frame(cbind(date,assign.type))

library(ggplot2)

ggplot(tmp, aes(x=as.numeric(date),fill=assign.type))+
  geom_density(alpha=0.8)

ggplot(tmp, aes(x=as.numeric(date),y=..count..,fill=assign.type, ymax=max(..count..+10)))+
  geom_freqpoly(binwidth=5)      

ggplot(tmp, aes(x=as.numeric(date)))+
  geom_area(aes(y = ..count.., fill = assign.type, group = assign.type), stat = "bin",position="dodge")

ggplot(tmp, aes(x=as.numeric(date),fill=assign.type))+
  geom_bar(position="dodge", binwidith=10)


Thanks!
Nick

baptiste auguie

unread,
Jun 21, 2013, 4:15:15 PM6/21/13
to Nick, ggp...@googlegroups.com
Hi,

do you mean

ggplot(tmp, aes(x=as.numeric(date),fill=assign.type))+
    stat_density(aes(y=..count..), alpha=0.8)

?

HTH,

b.

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
 
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nick

unread,
Jun 21, 2013, 4:43:39 PM6/21/13
to ggp...@googlegroups.com
Wow! That was embarrassingly obvious now that you pointed that out Baptiste. Yes that is exactly what I was looking for. Thank you. -Nick
Reply all
Reply to author
Forward
0 new messages