ggplot2中只显示两种密度中的一种 [英] Only one of two densities is shown in ggplot2
问题描述
dat <-data.frame(dens = c(nEXP,nCNT),lines = rep(c(Exp,Cont)))
ggplot(dat,aes(x = dens, group = lines,fill = lines))+ geom_density(alpha = .5)
代码它吐出关于不同长度的错误,例如
参数意味着不同的行数:x,y然后,我将代码扩充为: / b>
dat < - data.frame(dens = c(nEXP,nCNT),lines = rep(c(Exp, Cont),X))
其中X是较长参数的长度, 线条将与窝点相匹配。
现在的问题是,当我去绘制数据时,我只能得到一个密度图......我知道应该有两个,因为用密度/线绘制密度,显然是两个不相等的重叠分布,所以我假设错误i s与分组... ...
希望有道理。
<所以我不知道为什么,但基本上我只需手动执行rep()函数:
A< -data (A)<-c(a,key).frame(ExpN,key =exp)
B <-data.frame(ConN,key =con)
colnames )
colnames(B)<-c(a,key)
dat <-rbind(A,B)
ggplot(dat,aes(x = dens, fill = key))+ geom_density(alpha = .5)
So I have two sets of data (of different length) that I am trying to group up and display the density plots for:
dat <- data.frame(dens = c(nEXP,nCNT),lines = rep(c("Exp","Cont")))
ggplot(dat, aes(x = dens, group=lines, fill = lines)) + geom_density(alpha = .5)
when I run the code it spits an error about the different lengths, i.e. "arguments imply different num of rows: x, y"
I then augment the code to:
dat <- data.frame(dens = c(nEXP,nCNT),lines = rep(c("Exp","Cont"),X))
Where X is the length of the longer argument so the lengths of "lines" will match that of dens.
Now the issue is that when when I go to plot the data I am only getting ONE density plot.... I know there should be two, since plotting the densities with plot/lines, is clearly two non-equal overlapping distributions, so I am assuming the error is with the grouping...
hope that makes sense.
So I am not sure why but basically I simply had to do the rep() function manually:
A<-data.frame(ExpN, key = "exp")
B<-data.frame(ConN,key = "con")
colnames(A) <- c("a","key")
colnames(B) <- c("a","key")
dat <- rbind(A,B)
ggplot(dat, aes(x = dens, fill = key)) + geom_density(alpha = .5)
这篇关于ggplot2中只显示两种密度中的一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!