Question

I have some data:

dat <- data.frame(x=rnorm(100,100,100),y=rnorm(100,100,100))

I can plot it with a local trend line:

ggplot(dat, aes(x,y)) + stat_smooth()

But I want to overlay a density curve, on the same plot, showing the distribution of x. So just add the previous graph to this one (the y-axis is different, but I only care about relative differences in the density curve anyway):

ggplot(dat, aes(x)) + geom_density()

I know there's stat_binhex() and stat_sum() etc showing where the data falls. There are only a few y values, so what gets plotted by stat_binhex() etc is hard to read.

Was it helpful?

Solution

You can plot a combination of histograms and density curves at both sides of the scatterplot. In the example below I also included a confidence ellipse:

require(ggplot2)
require(gridExtra)
require(devtools)
source_url("https://raw.github.com/low-decarie/FAAV/master/r/stat-ellipse.R") # in order to create a 95% confidence ellipse

htop <- ggplot(data=dat, aes(x=x)) + 
  geom_histogram(aes(y=..density..), fill = "white", color = "black", binwidth = 2) + 
  stat_density(colour = "blue", geom="line", size = 1.5, position="identity", show_guide=FALSE) +
  scale_x_continuous("x-var", limits = c(-200,400), breaks = c(-200,0,200,400)) + 
  scale_y_continuous("Density", breaks=c(0.0,0.01,0.02), labels=c(0.0,0.01,0.02)) + 
  theme_bw() + theme(axis.title.x = element_blank())

blank <- ggplot() + geom_point(aes(1,1), colour="white") +
  theme(axis.ticks=element_blank(), panel.background=element_blank(), panel.grid=element_blank(),
        axis.text.x=element_blank(), axis.text.y=element_blank(), axis.title.x=element_blank(), axis.title.y=element_blank())

scatter <- ggplot(data=dat, aes(x=x, y=y)) + 
  geom_point(size = 0.6) + stat_ellipse(level = 0.95, size = 1, color="green") +
  scale_x_continuous("x-var", limits = c(-200,400), breaks = c(-200,0,200,400)) + 
  scale_y_continuous("y-var", limits = c(-200,400), breaks = c(-200,0,200,400)) + 
  theme_bw()

hright <- ggplot(data=dat, aes(x=y)) + 
  geom_histogram(aes(y=..density..), fill = "white", color = "black", binwidth = 1) + 
  stat_density(colour = "red", geom="line", size = 1, position="identity", show_guide=FALSE) +
  scale_x_continuous("y-var", limits = c(-200,400), breaks = c(-200,0,200,400)) + 
  scale_y_continuous("Density", breaks=c(0.0,0.01,0.02), labels=c(0.0,0.01,0.02)) +
  coord_flip() + theme_bw() + theme(axis.title.y = element_blank())

grid.arrange(htop, blank, scatter, hright, ncol=2, nrow=2, widths=c(4, 1), heights=c(1, 4))

the result: enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top