Skip to content

Instantly share code, notes, and snippets.

@lejarx
Forked from gufodotto/gist:2698960
Created February 1, 2016 03:31
Show Gist options
  • Save lejarx/7eedfaf4612671d58a14 to your computer and use it in GitHub Desktop.
Save lejarx/7eedfaf4612671d58a14 to your computer and use it in GitHub Desktop.
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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment