我们可以使用xtabs
将数据转换为宽格式,然后应用barplot
barplot(xtabs(Score ~ Grade + Class, data1), legend = TRUE,
col = c('yellow', 'red', 'orange'))
或使用 ggplot
library(dplyr)
library(ggplot2)
data1 %>%
mutate_at(vars(Grade, Class), factor) %>%
ggplot(aes(x = Grade, y = Score, fill = Class)) +
geom_col()
如果我们想为了让“类”,并转换为factor
与levels
该订单中指定基于“分数”值
data1 %>%
mutate(Class = factor(Class, levels = unique(Class[order(Score)])),
Grade = factor(Grade)) %>%
ggplot(aes(x = Grade, y = Score, fill = Class)) +
geom_col()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句