1

我有一些数据:

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

我可以用局部趋势线绘制它:

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

但我想在同一个图上叠加一条密度曲线,显示x. 所以只需将上一张图添加到这张图(y轴不同,但我只关心密度曲线的相对差异):

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

我知道有stat_binhex()stat_sum()显示数据落在哪里。只有几个y值,所以 etc 绘制的stat_binhex()内容很难阅读。

4

1 回答 1

2

您可以在散点图的两侧绘制直方图和密度曲线的组合。在下面的示例中,我还包括了一个置信椭圆:

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))

结果: 在此处输入图像描述

于 2014-05-13T20:31:18.293 回答