ggPlot2: Histogram with jittered stripchart
[This article was first published on Statistic on aiR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Here is an example of a Histogram plot, with a stripchart (vertically jittered) along the x side of the plot.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Alternatively, using the geom_rug function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
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.
To leave a comment for the author, please follow the link and comment on their blog: Statistic on aiR.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.