[英]How can I plot a filled histogram and its density with ggplot2?
我有用ggplot2創建的直方圖:
x = rnorm(100,0,150)
df <- data.frame(val=c(x))
ggplot(df, aes(val,..density.., fill = ..x..>100)) +
geom_histogram(binwidth=bw.SJ(df$val), colour="black") +
scale_fill_hue(h=c(115,230))
我想將pdf添加到此直方圖中,但是在添加下一行時:
geom_density(colour="red", lwd=1) +
這將返回錯誤:
Error in get(x, envir = this, inherits = inh)(this, ...) :
Aesthetics can not vary with a ribbon
提前致謝!
如果您指定它應該fill = ..x..>100)
內geom_histogram
而不是針對整個情節。 您不能在密度上改變填充顏色。
ggplot(df, aes(val)) +
geom_histogram(aes(fill = ..x.. > 100),
binwidth = bw.SJ(df$val), colour = "black") +
scale_fill_hue(h = c(115, 230)) +
geom_density(colour = "red", lwd = 1)
它在密度調用中不帶任何參數的情況下是否有效? 如果是這樣,請嘗試
geom_density(colour="red", size=1) +
代替
geom_density(colour="red", lwd=1) +
如果那不起作用,那么一個可重現的示例可能會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.