ggplot2中的重叠密度图 [英] Overlapped density plots in ggplot2

查看:337
本文介绍了ggplot2中的重叠密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个不同长度的矢量。我想要生成一个密度为两个矢量重叠的图。我认为我应该做的是:

  vec1 < -  data.frame(x = rnorm(2000,0,1 ))
vec2 < - data.frame(x = rnorm(3000,1,1.5))
ggplot()+ geom_density(aes(x = x,color =red),data = vec1)+
geom_density(aes(x = x,color =blue),data = vec2)

然后我认为我应该这样做:

$ $ $ $ code $ vec1< - data.frame(x = rnorm(2000 ,0,1))
vec2 < - data.frame(y = rnorm(3000,1,1.5))
ggplot()+ geom_density(aes(x = x,color =red ),data = vec1)+
geom_density(aes(x = y,color =blue),data = vec2)

这些都不起作用,因为颜色混淆了。

基于我在StackOverflow中找到的另一个解决方案1 2 ,我意识到我应该试试这个:

  vec1 < -  data.frame(x = rnorm(2000,0,1),grp =vec1)
vec2 < - data.frame(x =(rnorm(3000,1,1.5),grp =vec2)
allDat <-rbind(vec1,vec2)

ggplot(allDat,aes(x,color = grp) )+ geom_density()

ggplot(allDat,aes(x,color = grp))+ geom_density()+
scale_colour_manual(values = c(green,blue))

ggplot(allDat,aes(x,color = grp))+ geom_density()+
scale_colour_manual(values = c(vec2 =green,vec1 =blue))

好的,我解决了我原来的问题。但是有没有办法做类似于我上面尝试的第一个?从 ggplot 文档中的内容,我会这样想的。感谢任何建议。

解决方案

如果您移动颜色的分配 aes()

  vec1< c $ c> ;  -  data.frame(x = rnorm(2000,0,1))
vec2< - data.frame(x = rnorm(3000,1,1.5))

library( ggplot2)

ggplot()+ geom_density(aes(x = x),color =red,data = vec1)+
geom_density(aes(x = x),color =蓝色,data = vec2)


Imagine I have two vectors each of different length. I want to generate one plot with the density of both vectors overlaid. What I thought I should do is this:

vec1 <- data.frame(x=rnorm(2000, 0, 1))
vec2 <- data.frame(x=rnorm(3000, 1, 1.5))
ggplot() + geom_density(aes(x=x, colour="red"), data=vec1) + 
  geom_density(aes(x=x, colour="blue"), data=vec2)

Then I thought I should do this:

vec1 <- data.frame(x=rnorm(2000, 0, 1))
vec2 <- data.frame(y=rnorm(3000, 1, 1.5))
ggplot() + geom_density(aes(x=x, colour="red"), data=vec1) + 
  geom_density(aes(x=y, colour="blue"), data=vec2)

Neither of these quite work, because the colors get mixed up.

Based on another solution I found in StackOverflow 1 2, I realized I should try this:

vec1 <- data.frame(x=rnorm(2000, 0, 1), grp="vec1")
vec2 <- data.frame(x=rnorm(3000, 1, 1.5), grp="vec2")
allDat <- rbind(vec1, vec2)

ggplot(allDat, aes(x, colour=grp)) + geom_density()

ggplot(allDat, aes(x, colour=grp)) + geom_density() + 
  scale_colour_manual(values=c("green", "blue"))

ggplot(allDat, aes(x, colour=grp)) + geom_density() + 
  scale_colour_manual(values=c(vec2="green", vec1="blue"))

OK, I solved my original problem. But is there a way to do something akin to the first one I tried above? From the way things are worded in the ggplot documentation, I would have thought so. Appreciate any suggestions.

解决方案

Everything will work fine if you move the assignment of the colour parameter out of aes().

vec1 <- data.frame(x=rnorm(2000, 0, 1))
vec2 <- data.frame(x=rnorm(3000, 1, 1.5))

library(ggplot2)

ggplot() + geom_density(aes(x=x), colour="red", data=vec1) + 
  geom_density(aes(x=x), colour="blue", data=vec2)

这篇关于ggplot2中的重叠密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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