【发布时间】:2020-04-11 13:21:38
【问题描述】:
这是我的任务:
创建以下图表:
- 按 type1 和 is_legendary 对 Pokemon 数据进行分组
- 用攻击的平均值总结数据
- 创建一个条形图,x 为 type1,y 为攻击的平均值,然后输入其中一个填充
- 将类型一的颜色更改为 type_color
- facet_wrap 以便常规和传奇口袋妖怪有不同的条形图
我能够通过攻击的平均值来总结数据,但它不是我数据集中的一列。我如何做到这一点,所以攻击的意思是 y 美学?我是否忽略了一种更简单的方法?
我尝试在原始 Pokemon 数据集上创建一个新列,但这会创建所有数据的平均值,而不是按 type1 或 is_legendary 分组。
我也不断收到错误:错误:stat_count() 不得与任何美学一起使用。 但是当我查看该错误时,我看不出它如何应用于这个特定问题.
pokemon$type1 <- factor(pokemon$type1)
pokemon$is_legendary <- factor(pokemon$is_legendary)
pokemon %>%
group_by(type1, is_legendary) %>%
summarize(mean_attack = mean(attack)) %>%
ggplot(mapping = aes(x = type1, y = mean_attack, fill = type1)) + geom_bar()
+ scale_fill_manual(values = type_color) + facet_wrap(~ is_legendary)
+ labs(title = "Average Attack of Legendary and Regular Pokemon") + pokemon.theme
【问题讨论】:
-
一个小提示,如果您将汇总数据通过管道传输到
ggplot(),您可能不想同时将原始pokemon数据框作为您的数据源。 -
如果您想给出 y 坐标,请使用
geom_col。geom_bar用于当您希望 ggplot 为您汇总计数时 -
这里是one 的几个 SO 帖子,您可以通过搜索错误消息获得。除此之外,您可以将其设为reproducible example