ggplot2中只显示两种密度中的一种 [英] Only one of two densities is shown in ggplot2

查看:138
本文介绍了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屋!

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