我想要绘制一个基于数量的堆积条形图,但是标签上应该显示出该图表中所占比例。我已经生成了下面这张图表,但百分比是基于所有数据的。我想要的是按团队计算的百分比(例如,澳大利亚的百分比总和=100%,英格兰的百分比总和也是100%)。
为了实现这个目标,以下是需要使用的代码。该函数会统计5场比赛中每个团队中不同角色的数量(由于每场比赛中每位球员的角色出现两次(5场比赛x2次出场),因此必须将结果除以10):
任何帮助都将不胜感激。谢谢。
![Team plot](https://istack.dev59.com/g9d7f.webp)
team_roles_Q51 <- function(){
ashes_df <- tidy_data()
graph <- ggplot(ashes_df %>%
count(team, role) %>% #Groups by team and role
mutate(pct=n/sum(n)), #Calculates % for each role
aes(team, n, fill=role)) +
geom_bar(stat="identity") +
scale_y_continuous(labels=function(x)x/10) + #Needs to be a better way than dividing by 10
ylab("Number of Participants") +
geom_text(aes(label=paste0(sprintf("%1.1f", pct*100),"%")),
position=position_stack(vjust=0.5)) +
ggtitle("England & Australia Team Make Up") +
theme_bw()
print(graph)
}
导入的数据框如下所示:
数据框前10行的结构如下:
structure(list(batter = c("Ali", "Anderson", "Bairstow", "Ball",
"Bancroft", "Bird", "Broad", "Cook", "Crane", "Cummins"), team = structure(c(2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("Australia",
"England"), class = "factor"), role = structure(c(1L, 3L, 4L,
3L, 2L, 3L, 3L, 2L, 3L, 3L), .Label = c("allrounder", "batsman",
"bowler", "wicketkeeper"), class = "factor"), innings = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("test_1_innings_1",
"test_1_innings_2", "test_2_innings_1", "test_2_innings_2", "test_3_innings_1",
"test_3_innings_2", "test_4_innings_1", "test_4_innings_2", "test_5_innings_1",
"test_5_innings_2"), class = "factor"), batting_num = c(6, 11,
7, 10, 1, NA, 9, 1, NA, 9), score = c(38, 5, 9, 14, 5, NA, 20,
2, NA, 42), balls_faced = c(102, 9, 24, 11, 19, NA, 32, 10, NA,
120)), row.names = c(NA, 10L), class = "data.frame")
任何帮助都将不胜感激。谢谢。
mutate()
之前添加group_by(team)
:ashes_df %>% count(team, role) %>% group_by(team) %>% mutate(pct=n/sum(n))
。在这种情况下,这与ggplot2
没有什么关系。 - MrFlick