[英]Histograms and Density Plots do not match up
我正在創建替換的直方圖:時間上的第一,第二或第三子。 因此,每個直方圖都會在給定的“子編號”下顯示給定分鍾內的子編號。 直方圖對我來說很有意義,因為在大多數情況下它們都很平滑(我使用了1分鍾的紙箱寬度)。 沒有什么看起來與眾不同。 但是,當我覆蓋密度圖時,左側的尾巴會膨脹,因此我無法確定其中一張圖的原因。
數據集包含替換,范圍從1分鍾到最大時間。 然后,我將此數據集切成兩半,只看第45分鍾后制作潛艇的時間。我沒有將數據折回去,並且嘗試創建可復制的示例,但無法給出數據。
用於在R中創建的代碼
## Filter out subs that are not in the second half
df.half<-df[df$PeriodId>=2,]
p<-ggplot(data=df.half, aes(x=time)) +
geom_histogram(aes(y=..density..),position="identity", alpha=0.5,binwidth=1)+
geom_vline(data=sumy.df.half,aes(xintercept=grp.mean),color="blue", linetype="dashed", size=1)+
geom_density(alpha=.2)+
facet_grid(SUB_NUMBER ~ .)+
scale_y_continuous(limits = c(0,0.075),breaks = c(seq(0,0.075,0.025)),
minor_breaks = c(seq(0,0.075,0.025)),name='Count')
p
為什么對於第一個Sub,如果不小於45,則密度圖在尾部膨脹? 同樣,為什么第二子的尾部的密度圖不更膨脹?
旁注:我確實在交叉驗證中問過這個問題,但由於涉及R而被告知,請在此處提問。 這里
因此,我能夠更改代碼並獲得以下內容:
ggplot() +
geom_histogram(data=df.half, aes(x=time,y=..density..),position="identity", alpha=0.5,binwidth=1)+
geom_density(data=df.half,aes(x=time,y=..density..))+
geom_vline(data=sumy.df.half,aes(xintercept=grp.mean),color="blue", linetype="dashed", size=1)+
facet_grid(SUB_NUMBER ~ .)
這看起來更正確,並且至少現在適合數據集。 但是,對於為什么這些問題首先發生,我仍然感到困惑。
盡管沒有數據示例可重現該錯誤,但是您可以通過明確指定它來嘗試確保geom_density
使用的環境正確。 您也可以嘗試移動代碼行指定密度( geom_density
)就在后geom_histogram
。 另外, y軸標簽可能是錯誤的-現在將其設置為計數 ,而值表明實際上是密度 。
如何明確指定密度?
您可以通過在geom_density
函數調用中直接指定data
, aes
和position
來顯式指定密度參數,因此它將使用這些聲明的參數代替繼承的參數:
ggplot() +
geom_histogram(data=df.half, aes(x=time,y=..density..),position="identity", alpha=0.5,binwidth=1)+
geom_density(data=df.half,aes(x=time,y=..density..))+
geom_vline(data=sumy.df.half,aes(xintercept=grp.mean),color="blue", linetype="dashed", size=1)+
facet_grid(SUB_NUMBER ~ .)
我不了解它最初是如何發生的,我認為在您的geom_density
初始代碼中,您僅明確指定了alpha
參數。 因此,對於它需要的所有其他參數( data
, aes
, position
等),它使用了繼承的參數/參數,顯然它沒有正確地繼承它們。 可能是它試圖使用geom_vline
函數的data參數sumy.df.half
,或者被語法“ ..density ..”中的語法混淆了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.