有一个选项可以更改fill,并且col使用时geom_col
是否可以更改 geom_col 中的条形宽度 - 就像下面最后一行一样?
dt <- data.table(diamonds) [ , .(total=.N, price = mean(price)), by = cut]; dt # data.table to work with
ggplot(dt) + geom_col(aes(x=cut, y=price, fill=total)) # we can do this
ggplot(dt) + geom_point(aes(x=cut, y=price, size=total)) # we can do this
ggplot(dt) + geom_col(aes(x=cut, y=price, size=total)) # this does something different
ggplot(dt) + geom_col(aes(x=cut, y=price, width=total)) # this does not work
Run Code Online (Sandbox Code Playgroud)
或者什么可能是实现所需输出的方法 - 我需要条形的宽度与total.
这是一种非常典型的情况:当您绘制有关数据的任何内容时 - 您需要显示这些数据的样本大小
尝试这个:
library(ggplot2)
library(data.table)
ggplot(dt) +
geom_col(aes(x=cut, y=price), width = dt$total/100000)
Run Code Online (Sandbox Code Playgroud)
改变宽度参数的分母可以改变列的绝对宽度。

由reprex 包(v0.3.0)于 2020-06-13 创建
我接受了上面的答案,基于此编写了下面的代码,根据需要完成这项工作 - 通过将width论点转移到美学中:
dt %>% ggplot(dt) +
geom_col( aes(x=cut, y=price, width = total/max(total)) )
Run Code Online (Sandbox Code Playgroud)
这很好,因为您可以使用管道在管道中调用它%>%,因为您不需要dt$在函数内部指定。
但请注意,这会生成警告
Warning: Ignoring unknown aesthetics: width(使用 ggplot2_2.2.1 版本),但它确实可以完成这项工作。