![](/img/trans.png)
[英]How to customize the range of values in a y-axis of a geom_bar plus geom_error bar plot
[英]How to color geom_bar by y-axis values?
你可能會使用cut
ggplot(mtcars, aes(x = as.factor(cyl))) +
geom_bar(aes(fill = cut(stat(count), breaks = c(0, 8, 12, Inf)))) +
labs(fill = "Breaks")
你顯然需要根據你的需要/數據調整休息時間。
編輯
如果我們使用geom_col
而不是geom_bar
我們需要將fill = stat(count)
更改為fill = value_column
(無論value_column
是什么,但它應該與我們映射到y
那個相同)。
例:
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
ggplot(df, aes(trt, outcome)) +
geom_col(aes(fill = cut(outcome, breaks = c(0, 2, 3, Inf)))) +
labs(fill = "Breaks")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.