问题中的代码产生“总和”,因为geom_col()
默认为position = "stack"
.
以下是生成显示均值的图形的不同可能方法:
library(ggplot2)
# the normal way of plotting data summaries like means is to use stat_summary()
ggplot(data = midwest, mapping = aes(x = state, y = percwhite)) +
stat_summary(geom = "col", fun = mean)
# same plot using less intuitive code (avoid if possible)
ggplot(data = midwest, mapping = aes(x = state, y = percwhite)) +
geom_bar(stat = "summary", fun = mean)
# same plot using base R functions to pre-compute the means
means.df <- aggregate(percwhite ~ state, FUN = mean, data = midwest)
ggplot(data = means.df, mapping = aes(x = state, y = percwhite)) +
geom_col() # one value per column, stacking has no effect
rm(means.df) # assuming it is no-longer needed
# same plot using pipes and dplyr "verbs"
library(dplyr)
midwest %>%
group_by(state) %>%
summarise(percwhite = mean(percwhite)) %>%
ggplot(mapping = aes(x = state, y = percwhite)) +
geom_col()
需要注意的是,geom_bar()
和较新geom_col()
的非常相似。但是,只有geom_bar()
参数stat
和fun
定义。