【发布时间】:2021-03-15 02:17:59
【问题描述】:
我正在尝试根据计数绘制堆积条形图,但标签会显示图上的百分比。我已经制作了下面的情节。但是,百分比是基于所有数据。我所追求的是球队的百分比(例如澳大利亚的百分比之和 = 100% 和英格兰的百分比 = 100%)。
实现这一点的代码是以下函数。此函数计算 5 场比赛中每支球队中不同角色的数量(我不得不将结果除以 10,因为每场比赛中球员角色出现两次(5 场比赛 x 2 次出场):
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)
}
导入的数据框示例如下:
dataframe前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)). This doesn't really have anything to withggplot2` 在这种情况下。