我有以下数据。表
Golds Bronzes Silvers Country
1: 930 639 728 USA
2: 247 320 284 GER
3: 192 234 212 FRA
,我想在country
中使用x-axis
在y-axis
中使用奖牌数量来绘制组条形图。对于每个国家/地区,图表均应具有3个指示金,银和铜的条形图。有没有一种方法可以使用ggplot wnad进行操作而不会熔化数据?
答案 0 :(得分:1)
标准barplot
函数接受高度矩阵:
barplot(as.matrix(x[, 1:3]), beside = TRUE,
legend.text = x$Country)
更新:要对其进行另一种绘制,可以转置矩阵:
barplot(t(as.matrix(x[, 1:3])),
beside = TRUE,
names.arg = x$Country,
legend.text = names(x)[1:3])
答案 1 :(得分:0)
test <- data.frame(Country = c("USA", "GER", "FRA"),
Golds = c(930, 247, 192),
Bronzes = c(639, 320, 234),
Silvers = c(728, 284, 212))
test %>%
gather(key = "award", value = "number", -Country) %>%
ggplot(aes(x = Country, y = number, color = award, fill = award)) +
geom_col(position = "dodge")