Wednesday, February 5, 2014

ggPlot2: Histogram with jittered stripchart

Here is an example of a Histogram plot, with a stripchart (vertically jittered) along the x side of the plot.

library(ggplot2)
movies <- movies[1:1000,]
m <- ggplot(movies, aes(x=rating))
m + geom_histogram(binwidth=0.2, colour = "darkgreen", fill = "white", size=1) + geom_point(aes(y = -2), position = position_jitter(height = 0.8), size=1)
view raw Hist1 hosted with ❤ by GitHub




Alternatively, using the geom_rug function:

library(ggplot2)
movies <- movies[1:1000,]
m <- ggplot(movies, aes(x=rating))
m + geom_histogram(binwidth=0.2, colour = "darkgreen", fill = "white", size=1) + geom_rug(aes(y=-2), position="jitter", sides="b")
view raw hist2 hosted with ❤ by GitHub



Of course this simplicistic method need to be adjusted in vertical position of the stripchart or rugchart (y=-2, here), and the relative proportion of points jittering.

2 comments:

  1. can you also overlay a normal curve over the histograms, please

    ReplyDelete
  2. I'm a fan of ggplot2 and I like this post! So thank you!

    @Duleep Samuel : to easily overlay normal curve over histograms, you can use easyGgplot2 package. See this article : http://www.sthda.com/english/wiki/ggplot2-histogram-easy-histogram-graph-with-ggplot2-r-package

    ReplyDelete