![](/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.