2014-09-27 74 views
4

我试图在一个geom_density()图中绘制3组。geom_density()图中的多个组

的数据是在长格式:

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 

区域是可变的,所以是一个南和东北值,以及,代码如下:

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

Plot but with only one density not 3

更近一步

enter image description here

enter image description here

+0

好吧,我猜的,而不是我需要填写.... – Methexis 2014-09-27 13:47:39

+0

组有一个回答你的问题在这里[http://www.r-bloggers.com/density-plot -with-ggplot /](http://www.r-bloggers.com/density-plot-with-ggplot/) – 2015-11-02 15:46:30

回答

8

尝试以下操作:

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

enter image description here

如果你只想要的颜色,没有填充:

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

enter image description 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 
> 

图给出了只有一个曲线与https://dl.dropboxusercontent.com/u/16400709/StackOverflow/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 
+0

好吧,以上示例工作,这里是原始数据https://dl.dropboxusercontent .com/u/16400709/StackOverflow/DataStackGraph.csv让我现在你的想法? – Methexis 2014-09-27 14:23:31

+0

知道*嗯,仍然没有运气...我不知道ggplot2有主题! – Methexis 2014-09-27 14:37:53

+1

您的数据具有3组相同的值。请参阅上面我的回答中附加的解释。 – rnso 2014-09-27 14:56:08

4

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

`

library(httr) 
library(ggplot2) 

tmp <- GET("https://dl.dropboxusercontent.com/u/16400709/StackOverflow/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 
+0

aaaah该死的应该不是这种情况!谢谢 – Methexis 2014-09-27 14:50:54

+1

@hrbrmstr:很好的演示。 – rnso 2014-09-27 15:04:04

+0

谢谢大家的帮助,我只是重新开始这个过程,让这些数字出现,并且这次似乎已经奏效了! – Methexis 2014-09-27 15:22:47