[英]Add a continuous scale ggplot bar with stat_summary(fun.y = “mean”)
我想創建一個條形圖,按因子總結我的數據平均值。 然后,我想使條形圖的填充顏色反映均值。 我無法填補工作。 我認為這與stat_summary()
。
使用diamonds
數據集:
p <- ggplot(data = diamonds,
aes(x = cut, y = price, fill = price)) +
stat_summary(fun.y = "mean", geom= "bar" )
p
這是一種方法:
p <- ggplot(data = diamonds,
aes(x = cut, y = price)) +
stat_summary(fun.y = "mean", geom= "bar", aes(fill = ..y..))
在某些數據上調用統計摘要時,轉換后的數據與原始數據的名稱不同,您可以通過制作一個ggplot對象並對其調用ggplot_build
來進行檢查。
head(ggplot_build(p)$data[[1]])
fill x group ymin y ymax PANEL xmin xmax colour size linetype alpha
1 #4894D0 1 1 0 4358.758 4358.758 1 0.55 1.45 NA 0.5 1 NA
2 #2D5F89 2 2 0 3928.864 3928.864 1 1.55 2.45 NA 0.5 1 NA
3 #306592 3 3 0 3981.760 3981.760 1 2.55 3.45 NA 0.5 1 NA
4 #56B1F7 4 4 0 4584.258 4584.258 1 3.55 4.45 NA 0.5 1 NA
5 #132B43 5 5 0 3457.542 3457.542 1 4.55 5.45 NA 0.5 1 NA
您可以使用語法..variable..
(例如..y..
)將在此數據中定義的某些變量用作映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.