geom_density()图中有多个组 [英] Multiple Groups in geom_density() plot

查看:822
本文介绍了geom_density()图中有多个组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



数据是长格式的:


$ b $我想在一个geom_density()图中绘制3组。 b

  MEI计数区域
-2.031 10 MidWest
-1.999 0 MidWest
-1.945 15 MidWest
-1.944 1 MidWest
-1.875 6 MidWest
-1.873 10 MidWest
-1.846 18 MidWest

Region是变量,所以还有一个South和NorthEast值,代码如下:

  ggplot(d,aes (x = d $ MEI,group = d $ region))+ 
geom_density(adjust = 2)+
xlab(MEI)+
ylab(Density)



更靠近一步



解决方案
$ $ $ $ $ $ c $ ggplot()+
geom_density(data = ddf,aes(x = MEI,group = Region,fill = Region),alpha = 0.5,adjust = 2)+
xlab(MEI)+
ylab(Density)



如果您只想要颜色并且没有填充:

  ggplot( )+ 
geom_density(data = ddf,aes(x = MEI,group = Region,color = Region),adjust = 2)+
xlab(MEI)+
ylab密度)+
theme_classic()



  dput(ddf)
结构(列表(MEI = c(-2.031,-1.999,-1.945,-1.944,-1.875,
-1.873,-1.846,-2.031,-1.999 ,-1.945,-1.944,-1.875,-1.873,
-1.846,-2.031,-1.999,-1.945,-1.944,-1.875,-1.873,-1.846,
-2.031,-1.999 ,-1.945,-1.944,-1.875, -1.873,-1.846),Count = c(10L,
0L,15L,1L,6L,10L,18L,10L,0L,15L,1L,6L,10L,0L,15L,
10L ,0L,15L,1L,6L,10L,10L,0L,15L,1L,6L,10L,18L),Region = c(MidWest,
MidWest,MidWest 南部,南部,南部,南部,南部 North,NorthEast,NorthEast,
NorthEast,NorthEast,NorthEast,NorthEast,NorthEast,
NorthEast,NorthEast,NorthEast)),.Names = c(MEI,Count,
Region),class =data.frame,row.names = c NA,-28L))

ddf
美元计数地区
1 -2.031 10中西部
2 -1.999 0中西部
3 -1.945 15中西部
4 -1.944 1 MidWest
5 -1.875 6 MidWest
6 -1.873 10 MidWest
7 -1.846 18 MidWest
8 -2.031 10 South
9 - 1.999 0南方
10 -1.945 15南方
11 -1.944 1南方
12 -1.875 6南部
13 -1.873 10南部
14 -1.846 0南部
15 -2.031 15南部
16 -1.999 10南部
17 - 1.945 0南部
18 -1.944 15东北部
19 -1.875 1东北部
20 -1.873 6东北部
21 -1.846 10东北部
22 -2.031 10东北部
23 -1.999 0 NorthEast
24 -1.945 15 NorthEast
25 -1.944 1 NorthEast
26 -1.875 6 NorthEast
27 -1.873 10 NorthEast
28 -1.846 18 NorthEast
>

图只给出一条来自 https://dl.dropboxusercontent.com/u/16400709/StackOverflow/DataStackGraph.csv ,因为所有3个因素具有相同的密度:

 > (dfmain,tapply(MEI,Region,mean))
MidWest Northeast South
0.1717846 0.1717846 0.1717846
>
> (dfmain,tapply(MEI,Region,sd))
MidWest Northeast South
1.014246 1.014246 1.014246
>
> (dfmain,tapply(MEI,Region,length))
MidWest Northeast North
441 441 441


I am trying to plot 3 groups in one geom_density()plot.

The data is in long format:

MEI Count   Region
-2.031  10  MidWest
-1.999  0   MidWest
-1.945  15  MidWest
-1.944  1   MidWest
-1.875  6   MidWest
-1.873  10  MidWest
-1.846  18  MidWest

Region is the variable, so there is a South and NorthEast value as well, code is below:

ggplot(d, aes(x=d$MEI, group=d$region)) + 
  geom_density(adjust=2) + 
  xlab("MEI") +
  ylab("Density")

a step closer

解决方案

Try following:

ggplot() + 
  geom_density(data=ddf, aes(x=MEI, group=Region, fill=Region),alpha=0.5, adjust=2) + 
  xlab("MEI") +
  ylab("Density")

If you only want color and no fill:

ggplot() + 
  geom_density(data=ddf, aes(x=MEI, group=Region, color=Region), adjust=2) + 
  xlab("MEI") +
  ylab("Density")+
  theme_classic()

Following data is used here:

dput(ddf)
structure(list(MEI = c(-2.031, -1.999, -1.945, -1.944, -1.875, 
-1.873, -1.846, -2.031, -1.999, -1.945, -1.944, -1.875, -1.873, 
-1.846, -2.031, -1.999, -1.945, -1.944, -1.875, -1.873, -1.846, 
-2.031, -1.999, -1.945, -1.944, -1.875, -1.873, -1.846), Count = c(10L, 
0L, 15L, 1L, 6L, 10L, 18L, 10L, 0L, 15L, 1L, 6L, 10L, 0L, 15L, 
10L, 0L, 15L, 1L, 6L, 10L, 10L, 0L, 15L, 1L, 6L, 10L, 18L), Region = c("MidWest", 
"MidWest", "MidWest", "MidWest", "MidWest", "MidWest", "MidWest", 
"South", "South", "South", "South", "South", "South", "South", 
"South", "South", "South", "NorthEast", "NorthEast", "NorthEast", 
"NorthEast", "NorthEast", "NorthEast", "NorthEast", "NorthEast", 
"NorthEast", "NorthEast", "NorthEast")), .Names = c("MEI", "Count", 
"Region"), class = "data.frame", row.names = c(NA, -28L))

 ddf
      MEI Count    Region
1  -2.031    10   MidWest
2  -1.999     0   MidWest
3  -1.945    15   MidWest
4  -1.944     1   MidWest
5  -1.875     6   MidWest
6  -1.873    10   MidWest
7  -1.846    18   MidWest
8  -2.031    10     South
9  -1.999     0     South
10 -1.945    15     South
11 -1.944     1     South
12 -1.875     6     South
13 -1.873    10     South
14 -1.846     0     South
15 -2.031    15     South
16 -1.999    10     South
17 -1.945     0     South
18 -1.944    15 NorthEast
19 -1.875     1 NorthEast
20 -1.873     6 NorthEast
21 -1.846    10 NorthEast
22 -2.031    10 NorthEast
23 -1.999     0 NorthEast
24 -1.945    15 NorthEast
25 -1.944     1 NorthEast
26 -1.875     6 NorthEast
27 -1.873    10 NorthEast
28 -1.846    18 NorthEast
> 

Graph gives only one curve with your own data from https://dl.dropboxusercontent.com/u/16400709/StackOverflow/DataStackGraph.csv since all 3 factors have identical densities:

> with(dfmain, tapply(MEI, Region, mean))
  MidWest Northeast     South 
0.1717846 0.1717846 0.1717846 
> 
> with(dfmain, tapply(MEI, Region, sd))
  MidWest Northeast     South 
 1.014246  1.014246  1.014246 
> 
> with(dfmain, tapply(MEI, Region, length))
  MidWest Northeast     South 
      441       441       441 

这篇关于geom_density()图中有多个组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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