Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ggplot2: Create faceted scatterplot with scaled and moved density

Tags:

r

ggplot2

I would like to plot some data as a scatter plot using facet_wrap, while superimposing some information such as a linear regression and the density. I managed to do all that, but the density values are out of proportion with respect to my points, which is a normal thing since these points are far away. Nevertheless, I'd like to scale and move my density curve so that it is clearly visible; I don't care about it's real values but more about its shape.

Here is an exaggerated minimum working example of what I have:

set.seed(48151623)
mydf <- data.frame(x1=rnorm(mean=5,n=100),x2=rnorm(n=100,mean=10),x3=rnorm(n=100,mean=20,sd=3))
mydf$var <- mydf$x1 + mydf$x2 * mydf$x3 
mydf.wide  <- melt(mydf,id.vars='var',measure.vars=c(1:3))
ggplot(data=mydf.wide,aes(x=value,y=var)) +
  geom_point(colour='red') +
  geom_smooth(method='lm') +
  stat_density(aes(x=value,y=..scaled..),position='identity',geom='line') +
  facet_wrap(~variable,scale='free_x')

Which results in: example of current plot

What I would like resembles to this ugly hack:

stat_density(aes(x=value,y=..scaled..*100+200),position='identity',geom='line')

Ideally, I would use y=..scaled..* diff(range(value)) + min(value) but when I do this I get an error saying that 'value' was not found. I suspect the problem is related to the faceting, but I would prefer to keep my facets.

How can I scale and move the density curve in this case?

cool result but ugly hack

like image 484
YuppieNetworking Avatar asked Feb 15 '23 22:02

YuppieNetworking


1 Answers

I suggest to make two plots and combine them with grid.arrange:

p1 <- ggplot(data=mydf.wide,aes(x=value,y=var)) +
  geom_point(colour='red') +
  geom_smooth(method='lm') +
  facet_wrap(~variable,scale='free_x') +
  theme(axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin =       unit(c(1, 1, 0, 0.5), "lines"))

p2 <- ggplot(data=mydf.wide,aes(x=value,y=var)) +
  stat_density(aes(x=value,y=..scaled..),position='identity',geom='line') +
  facet_wrap(~variable,scale='free_x') + 
  theme(strip.background=element_blank(),
        strip.text=element_blank(),
        plot.margin =       unit(c(-1, 1, 0.5, 0.35), "lines"))

library(gridExtra)
grid.arrange(p1, p2, heights = c(2,1))

enter image description here

like image 106
Roland Avatar answered Feb 27 '23 12:02

Roland