我有两组数据,我希望得到一个包含每组数据的直方图和频率多边形的图表.
我的数据框df如下:
'data.frame': 20000 obs. of 2 variables:
$ measure : num -0.566 0.321 0.125 1.353 -1.288 ...
$ processing: Factor w/ 2 levels "before","after": 1 1 1 1 1 1 1 1 1 1 ...
measure processing
1 -0.5656801 before
2 0.3210458 before
3 0.1252706 before
4 1.3532248 before
5 -1.2877305 before
6 0.3225545 before
Run Code Online (Sandbox Code Playgroud)
我的代码如下:
png("figure_%d.png")
set.seed(2014)
n <- 10000
before <- rnorm(n)
df_1 <- data.frame(measure=before)
df_1$processing <- factor("before")
after <- before-rnorm(n,mean=1,sd=0.1)
df_2 <- data.frame(measure=after)
df_2$processing <- factor("after")
df<-rbind(df_1,df_2)
library(ggplot2)
print(ggplot(df, aes(measure,colour=processing))+geom_freqpoly())
print(ggplot(df, aes(measure,fill=processing))+geom_density(alpha=0.5))
print(ggplot(df_1, aes(measure,fill=processing))+geom_histogram(alpha=0.5))
print(ggplot(df_2, aes(measure,fill=processing))+geom_histogram(alpha=0.5))
print(ggplot(df, aes(measure,fill=processing))+geom_histogram(alpha=0.5))
print(ggplot(df, aes(measure,fill=processing,colour=processing))+geom_freqpoly()+geom_histogram(alpha=0.5))
Run Code Online (Sandbox Code Playgroud)
现在,之后
ggplot(df, aes(measure,colour=processing))+geom_freqpoly()
Run Code Online (Sandbox Code Playgroud)
我得到下图

其中两个频率多边形是预期的.
后
ggplot(df, aes(measure,fill=processing))+geom_density(alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
我得到下图

并且两个密度重叠的地方我得到了预期的"混合"颜色.
现在我想得到一个带有两个直方图的数字; 首先,我在两个单独的图中绘制两个直方图:使用代码
ggplot(df_1, aes(measure,fill=processing))+geom_histogram(alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
我得到下图

和代码
ggplot(df_2, aes(measure,fill=processing))+geom_histogram(alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
我得到下图

这两个直方图都是预期的.
当我尝试使用此代码在同一图表中绘制直方图时,问题就出现了
ggplot(df, aes(measure,fill=processing))+geom_histogram(alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
我得到这个数字

我无法解释为什么绿色直方图高于红色直方图.此外,在两个直方图"重叠"的情况下,我没有得到"混合"颜色.
尝试添加频率多边形会使问题恶化,使用此代码
ggplot(df, aes(measure,fill=processing,colour=processing))+geom_freqpoly()+geom_histogram(alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
我得到这个数字

频率多边形在我看来是正确的,但直方图是错误的,如上图所示.
我究竟做错了什么?
输出version是
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 0.2
year 2013
month 09
day 25
svn rev 63987
language R
version.string R version 3.0.2 (2013-09-25)
nickname Frisbee Sailing
Run Code Online (Sandbox Code Playgroud)
输出sessionInfo()是
R version 3.0.2 (2013-09-25)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] methods stats graphics grDevices utils datasets base
other attached packages:
[1] ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4 grid_3.0.2
[5] gtable_0.1.2 labeling_0.2 MASS_7.3-29 munsell_0.4.2
[9] plyr_1.8 proto_0.3-10 RColorBrewer_1.0-5 reshape2_1.2.2
[13] scales_0.2.3 stringr_0.6.2
Run Code Online (Sandbox Code Playgroud)
geom_histogram与参数一起使用position = "identity".positionis 的默认值"stack".在这种情况下,条不重叠但是堆叠.
geom_histogram(alpha = 0.5, position = "identity")
Run Code Online (Sandbox Code Playgroud)
完整的代码:
library(ggplot2)
ggplot(df, aes(measure, fill = processing)) +
geom_histogram(alpha = 0.5, position = "identity")
Run Code Online (Sandbox Code Playgroud)
