简体   繁体   English

ggplot2中重叠的密度图

[英]Overlapped density plots in ggplot2

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: 根据我在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, 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. ggplot文档中的措辞ggplot ,我会这么认为。 Appreciate any suggestions. 感谢任何建议。

Everything will work fine if you move the assignment of the colour parameter out of aes() . 如果你将colour参数的赋值移出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)

在此输入图像描述

Try this if you want have legends too: 如果你想要传说,试试这个:

df <- rbind(data.frame(x=rnorm(2000, 0, 1), vec='1'),
            data.frame(x=rnorm(3000, 1, 1.5), vec='2'))
ggplot(df, aes(x, group=vec, col=vec)) + geom_density(position='dodge')

在此输入图像描述

I had some troubles with the the above solution, as my data was contained in a single data frame. 我对上述解决方案有些麻烦,因为我的数据包含在一个数据框中。 Using ... data=df$A in the aesthetics doesn't work as this will provide ggplot with a vector of class "numeric", which isn't supported. 在美学中使用... data=df$A不起作用,因为这将为ggplot提供类“vector”的向量,这是不受支持的。

Therefor, to overlay different columns all contained in the same data frame, I'd suggest: 因此,为了覆盖同一数据框中包含的不同列,我建议:

vec1 <- rnorm(3000, 0, 1)
vec2 <- rnorm(3000, 1, 1.5)

df <- data.frame(vec1, vec2)
colnames(df) <- c("A", "B")

library(ggplot2)

ggplot() + geom_density(aes(x=df$A), colour="red") + 
  geom_density(aes(x=df$B), colour="blue")

在此输入图像描述

For most people this might seem obvious, but for me as a beginner, it wasn't. 对于大多数人来说,这似乎是显而易见的,但对我来说,作为初学者,事实并非如此。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM