在本网站上非常有帮助的用户的帮助下,我有了这段代码。我们的目标是为学校每年的“水平”创建一个测试分数条形图,以创建一个视觉,让学生看到他们的分数与同学的分数相比如何,而不会透露谁得分。生成的图表将只包含一个学生的姓名和学生#xx,用于班级中的其他学生。以下代码适用于此数据样本,但当我将其应用于我的43个观察的完整数据集时,我收到以下错误:
"Error in mutate_impl(.data, dots) : Evaluation error: Column `n` must be length 43 (the number of rows) or one, not 42."
此外,我想在每个栏上添加所有百分比。
names <- c("Jackson", "Smith", "Johnson", "Richards", "Matthews", "Redmond", "Phillips")
scores <- c(.99, .65, .73, .89, .88, .92, .87)
level <- c(10,11,10,11,11,11,11)
grades <- cbind.data.frame(names, scores, level)
df <- 1:nrow(grades) %>%
purrr::map( ~grades) %>%
set_names(grades$names) %>%
bind_rows(.id = "ID") %>%
nest(-ID) %>%
mutate(level=map2(data,ID, ~.x %>% filter(names == .y) %>% select(level))) %>%
mutate(data=
map2(data, ID, ~.x %>%
mutate(n=paste0("#", sample(seq_len(n()), size = n())),
names=ifelse(names == .y, as.character(names), n),
names=factor(names, levels = c(.y, sample(n, n())))))) %>%
mutate(plots=map2(data,level, ~ggplot(data=.x,aes(x = names, y = scores, fill = scores))+
geom_col() +
ggtitle("test scores by level- February 2018", subtitle = .y$level)
))
library(cowplot)
plot_grid(df$plots[[1]], df$plots[[2]], df$plots[[3]],df$plots[[4]])