[英]How to plot, in the same graph, the histogram and the frequency polygon of two sets of data with ggplot2 in R
我有兩組數據,我想為每組數據獲取一個帶有直方圖和頻率多邊形的圖形。
我的數據框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
我的代碼如下:
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))
現在,之后
ggplot(df, aes(measure,colour=processing))+geom_freqpoly()
我得到下圖
其中兩個頻率多邊形符合預期。
后
ggplot(df, aes(measure,fill=processing))+geom_density(alpha=0.5)
我得到下圖
在兩個密度重疊的地方,我得到了預期的“混合”顏色。
現在,我想得到一個帶有兩個直方圖的圖形; 首先,我在兩個單獨的圖中繪制兩個直方圖:使用代碼
ggplot(df_1, aes(measure,fill=processing))+geom_histogram(alpha=0.5)
我得到下圖
並與代碼
ggplot(df_2, aes(measure,fill=processing))+geom_histogram(alpha=0.5)
我得到下圖
兩個直方圖均符合預期。
當我嘗試使用此代碼在同一張圖中繪制兩個直方圖時,問題就開始了
ggplot(df, aes(measure,fill=processing))+geom_histogram(alpha=0.5)
我得到這個數字
而且我無法解釋為什么綠色直方圖高於紅色直方圖。 此外,在兩個直方圖“重疊”的情況下,我沒有得到“混合”的顏色。
嘗試添加頻率多邊形使此問題惡化
ggplot(df, aes(measure,fill=processing,colour=processing))+geom_freqpoly()+geom_histogram(alpha=0.5)
我得到這個數字
頻率多邊形在我看來似乎是正確的,但直方圖是錯誤的,如上圖所示。
我究竟做錯了什么?
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
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
將geom_histogram
與參數position = "identity"
。 position
的默認值為"stack"
。 在這種情況下,條形不重疊而是堆疊在一起。
geom_histogram(alpha = 0.5, position = "identity")
完整的代碼:
library(ggplot2)
ggplot(df, aes(measure, fill = processing)) +
geom_histogram(alpha = 0.5, position = "identity")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.