geom_density() 图中的多个组

Posted

技术标签:

【中文标题】geom_density() 图中的多个组【英文标题】:Multiple Groups in geom_density() plot 【发布时间】:2014-11-22 09:17:11 【问题描述】:

我正在尝试在一个 geom_density() 图中绘制 3 个组。

数据为长格式:

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 是变量,所以还有 South 和 NorthEast 的值,代码如下:

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

更近一步

【问题讨论】:

好吧,我猜我需要填充而不是组...... 这里有你的问题的答案http://www.r-bloggers.com/density-plot-with-ggplot/ 【参考方案1】:

尝试以下操作:

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("Density")+
  theme_classic()

此处使用以下数据:

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
> 

图表只给出了一条曲线,其中包含您自己来自https://dl.dropboxusercontent.com/u/16400709/***/DataStackGraph.csv 的数据,因为所有 3 个因子的密度都相同:

> 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 

【讨论】:

好的,所以上面的例子有效,这是原始数据dl.dropboxusercontent.com/u/16400709/***/…现在让我谈谈你的想法? 知道* 嗯仍然没有运气...我不知道 ggplot2 有主题! 您的数据对于 3 个组具有相同的值。请参阅上面我的答案中附加的说明。【参考方案2】:

回应“知道*嗯仍然没有运气......”,这是因为它们都是一样的(见下文)。您应该接受并使用@mso 的回答。

library(httr)
library(ggplot2)

tmp <- GET("https://dl.dropboxusercontent.com/u/16400709/***/DataStackGraph.csv")

dat <- read.csv(textConnection(content(tmp, as="text")))

gg <- ggplot(data=dat)
gg <- gg + geom_density(aes(x=MEI, group=Region, fill=Region), 
                        alpha=0.5, adjust=2)
gg <- gg + facet_grid(~Region)
gg <- gg + labs("MEI", "Density")
gg <- gg + theme_bw()
gg

【讨论】:

@hrbrmstr:很好的证明。 感谢大家的帮助,我刚刚重新运行该过程以获取要绘制的数字,这一次似乎成功了!

以上是关于geom_density() 图中的多个组的主要内容,如果未能解决你的问题,请参考以下文章

将图例添加到 geom_density R [重复]

将 vline 添加到 geom_density 和平均 R 的阴影置信区间

一张图中的多个图

R仿图:Base plot仿geom_density()

R仿图:Base plot仿geom_density()

如何从 ggplot2::geom_density_2d_filled 获取有关轮廓的信息?