ggplot2:添加描述散点图两个维度的条件密度曲线 [英] ggplot2: add conditional density curves describing both dimensions of scatterplot

查看:1717
本文介绍了ggplot2:添加描述散点图两个维度的条件密度曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自两个类别的2D数据的散点图。我想补充密度线为每个维度 - 而不是阴谋(参见散点图与外部ggplot2中的边际直方图),但是在绘图表面上。

  set.seed(123)
dim1<< ; c(rnorm(100,mean = 1),rorm(100,mean = 4))
dim2 -norm(200,mean = 1)
cat < - factor rep(a,100),rep(b,100)))
mydf < - data.frame(cbind(dim2,dim1,cat))
ggplot(data = mydf, aes(x = dim1,y = dim2,color = as.factor(cat)))+
geom_point()+
stat_density(aes(x = dim1,y =( - 2+ ),
position =identity,geom =line)

看起来像这样:





但我想要一对类似的密度曲线垂直运行,显示y维中点的分布。我试过了

$ $ $ $ $ $ $ c $ stat_density(aes(y = dim2,x = 0 +(.. scaled ..))),position = identity,geom =line)

但收到错误stat_density需要以下缺失的美学: x。



任何想法?谢谢

解决方案

dim2变量的密度,然后翻转坐标轴并将它们存储在一个新的data.frame中,然后将它们绘制在另一个图的顶部。

 position =identity,geom =line)

stuff< - ggplot_build(p)
xrange< - stuff [[2]] $ ranges [[1]] $ x.range#提取x范围,使新的密度与y轴对齐

##获取dim2的密度
ds < - do.call(rbind,lapply(unique(mydf $ cat),function(lev)){
dens&l (x = dens $ y + xrange [1],y = dens $ x,cat = lev)
)))

p + geom_path(data = ds,aes(x = x,y = y,color = factor(cat)))
pre>


I have scatterplots of 2D data from two categories. I want to add density lines for each dimension -- not outside the plot (cf. Scatterplot with marginal histograms in ggplot2) but right on the plotting surface. I can get this for the x-axis dimension, like this:

set.seed(123)
dim1 <- c(rnorm(100, mean=1), rnorm(100, mean=4))
dim2 <- rnorm(200, mean=1)
cat <- factor(c(rep("a", 100), rep("b", 100)))
mydf <- data.frame(cbind(dim2, dim1, cat))
ggplot(data=mydf, aes(x=dim1, y=dim2, colour=as.factor(cat))) + 
  geom_point() +
  stat_density(aes(x=dim1, y=(-2+(..scaled..))), 
  position="identity", geom="line")

It looks like this:

But I want an analogous pair of density curves running vertically, showing the distribution of points in the y-dimension. I tried

stat_density(aes(y=dim2, x=0+(..scaled..))), position="identity", geom="line)

but receive the error "stat_density requires the following missing aesthetics: x".

Any ideas? thanks

解决方案

You can get the densities of the dim2 variables. Then, flip the axes and store them in a new data.frame. After that it is simply plotting them on top of the other graph.

p <- ggplot(data=mydf, aes(x=dim1, y=dim2, colour=as.factor(cat))) + 
  geom_point() +
  stat_density(aes(x=dim1, y=(-2+(..scaled..))), 
               position="identity", geom="line")

stuff <- ggplot_build(p)
xrange <- stuff[[2]]$ranges[[1]]$x.range  # extract the x range, to make the new densities align with y-axis

## Get densities of dim2
ds <- do.call(rbind, lapply(unique(mydf$cat), function(lev) {
    dens <- with(mydf, density(dim2[cat==lev]))
    data.frame(x=dens$y+xrange[1], y=dens$x, cat=lev)
}))

p + geom_path(data=ds, aes(x=x, y=y, color=factor(cat)))

这篇关于ggplot2:添加描述散点图两个维度的条件密度曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆