条形图颜色参数覆盖填充

Barplot colour parameter overrides fill

我想更改条形图的轮廓颜色,但每次设置颜色时它都会完全填满条形图。

ggplot(diamonds, aes(x=clarity, y=depth))+geom_bar(stat='identity',fill="#FF9999")

上面的代码工作正常 returns 一个粉红色的图,但是当我想在每个条形图周围添加一个黑色轮廓时。像这样..

ggplot(diamonds, aes(x=clarity, y=depth))+geom_bar(stat='identity',fill="#FF9999", colour='black')

每个条形图都变成黑色。我修改了 R Graphics Cookbook (O'Reilly) 第 21 页的示例代码。

我想我的评论加在一起是一个不错的答案:

在默认位置 = "stack" 的情况下,您正在做的是绘制一堆彼此重叠的小条,您可以通过

看到它们
ggplot(diamonds[1:100, ], aes(x=clarity, y = depth)) +
    geom_bar(fill="#FF9999", colour='black', stat = "identity")

对于大多数条形图,您只需指定一个 x 变量来获取 y 上的计数

ggplot(diamonds, aes(x=clarity)) +
    geom_bar(fill="#FF9999", colour='black')

如果您想总结每个清晰度值的深度,我会使用 dplyr。 (我从来没有掌握 stat_summary 的窍门,这是 ggplot 的方法。)

library(dplyr)
diamonds %>% group_by(clarity) %>% 
  summarize(mean_depth = mean(depth)) %>%
  ggplot(aes(x = clarity, y = mean_depth)) +
    geom_bar(stat = "identity", fill = "#FF9999", colour = "black")