在每个条形图上添加一个标签 [英] Adding one label to each bar graph r

查看:136
本文介绍了在每个条形图上添加一个标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试汇编来自50名学生的考试成绩的图形描述.我的最终目标是创建50个条形图的变体,每位学生一个,每个轴上只有一个学生的姓名,这样他/她就可以看到他们与其他人的比较而不透露谁得分.在下面的照片中,我想将"Jackson"放在第一个栏上,而将其他空白留给第一个版本.第二个将只有"Smith",等等.此外,我想根据他们在校的年份(变量"level")对数据进行拆分.

I am trying to compile a graphical depiction of test scores from 50 students. My ultimate goal would be to create 50 variations of the bar chart, one for each student, with only the one student's name on the axis so he/she can see how they compare to the others without disclosing who scored what. In the photo below, I would like to put "Jackson" on the first bar and leave the others blank for the first variation. The second would only have "Smith", etc. Additionally, I would like to split the data based on their year in school, the variable "level".

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)


Gradesplit <- split(grades, grades$level)
plotdata <- function(grades) {
              ggplot(data = grades, aes(x = names, y = scores, fill = scores))+ 
                geom_bar(stat = "identity", position = "dodge")+ 
                theme(axis.text.x=element_text(angle= 45, vjust=.5)) +
                ggtitle("test scores by level-  February 2018", grades$level)}

lapply(Gradesplit, plotdata)

推荐答案

我建议添加一些采样.这样,学生就无法找到模式并做出结论. 因此,您可以尝试

I recommend to add some sampling. Then the students can't find patterns and make conclusions. Thus, you can try

library(tidyverse)
# bring the data in adequat format
# In brief, a list of the same data.frame for each student
df <- 1:nrow(grades) %>% 
     purrr::map( ~grades) %>% 
     set_names(grades$names) %>% 
     bind_rows(.id = "ID") %>% 
     nest(-ID) %>%  
# the plots using purrr::map2  
     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)
     )) 
# and or illustration purposes the first four plots
library(cowplot)
plot_grid(df$plots[[1]], df$plots[[2]], df$plots[[3]],df$plots[[4]])

这篇关于在每个条形图上添加一个标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆