Plotting data and distribution simultaneously (with ggplot2)

[This article was first published on gRaphics!, 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.

Ever wanted to see at a glance the distribution of your data across different axes? It happens often to me, and R allows to build a nice plot composition – This is my latest concoction. I used ggplot2 here, but equivalent graphics can be made using either base graphics, or lattice.

The set is the usual ‘iris’, the central plot has petal length and width along the X/Y axes – I  used a customised color palette so as to be friendlier to color-blind people. On the left and at the top of the main plot, the density distribution of the whole set (grey) and by subspecies.

library(ggplot2);
library(grid);
data(iris)
x <- jitter(iris[,c('Sepal.Length')])
y <- jitter(iris[,c('Sepal.Width')])
z <- factor(iris[,c('Species')])
# The color blind palette without black:
cbnbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
df<-data.frame(x,y,z) # just create a dataframe - x y and z are easier to write than Petal.length and so on
# I now define the (gg)plots
# old p1 and p2 (..density.. plots)...
# p1<-ggplot(df) + scale_fill_manual(values=cbnbPalette) + geom_density(aes(x = x, y = -..density..), col='black', fill="#CCCCCC") + geom_density(aes(x = x, y = ..density.., fill = subsp, alpha=0.4)) +theme_invisible() + opts(legend.position = "none")
# p2<-ggplot(df) + scale_fill_manual(values=cbnbPalette) + geom_density(aes(x = y, y = -..density..), col='black', fill="#CCCCCC") + geom_density(aes(x = y, y = ..density.., fill = subsp, alpha=0.4)) +theme_invisible() + opts(legend.position = "none") +coord_flip()
# now susbstituted by ..count.. plots as suggested by Andrew in comments
p1<-ggplot(df) # based on the dataframe just defined
+ scale_fill_manual(values=cbnbPalette) # using the colorblind-friendly palette
+ geom_density(aes(x = x, y = -..count.., col=subsp), fill="#CCCCCCCC", position = "stack") # overall density plot - plotted on the negative
+ geom_density(aes(x = x, y = ..count.., fill = subsp, alpha=0.4)) # so as to be specular to the densities by subspecies
+ theme_invisible() # oh yeah, I don't want any other graphical element to crowd this plot - the x axys is the same as in the main plot
+ opts(legend.position = "none")
# this is a second density plot, oriented vertically (hence the 'coord_flip()' at the end
p2<-ggplot(df) + scale_fill_manual(values=cbnbPalette) + geom_density(aes(x = y, y = -..count.., col=subsp), fill="#CCCCCCCC", position = "stack") + geom_density(aes(x = y, y = ..count.., fill = subsp, alpha=0.4)) +theme_invisible() + opts(legend.position = "none") +coord_flip()
#finally the main x/y plot - nothing to write home about
p3<- ggplot(df) + scale_colour_manual(values=cbbPalette) + geom_point(aes(x = x, y = y, col=subsp)) + opts(legend.position = c(1.2,1.2))
#now let's print the plot to screen!
grid.newpage()
pushViewport(viewport(layout = grid.layout(5, 5))) # a 5 by 5 grid
print(p1, vp=vplayout(1,1:4)) # the first density plot will occupy the top of the grid
print(p3, vp=vplayout(2:5,1:4)) # the main x/y plot will instead spread across most of the grid
print(p2, vp=vplayout(2:5,5)) # with the second density plot occupying a narrow vertical strip at the right
# done! Enjoy!
view raw gistfile1.r hosted with ❤ by GitHub
Well, I hope the code is clear – this time I commented it a bit more…

To leave a comment for the author, please follow the link and comment on their blog: gRaphics!.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)