简体   繁体   English

为每个条形图添加一个标签r

[英]Adding one label to each bar graph r

I am trying to compile a graphical depiction of test scores from 50 students. 我正在尝试编译50名学生的测试分数的图形描述。 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. 我的最终目标是创建条形图的50种变体,每个学生一个,在轴上只有一个学生的名字,这样他/她就可以看到他们与其他学生的比较,而不会透露谁得分。 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 requires a bit of understanding, but we can make it work. 它需要一点理解,但我们可以使它工作。 As @richardtelford said, we need to manually build the labels, to do so we can 'loop' on the names, filter the by the level of each name, build a vector of appropriate legnth, and finally build the plot with such labels: 正如@richardtelford所说,我们需要手动构建标签,为此我们可以“循环”名称,按每个名称的级别过滤,构建适当的legnth矢量,最后使用这些标签构建图:

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)


library(purrr)
library(dplyr)
library(ggplot2)

grades$names %>% 
  walk(~{
    filtered_grades <- grades %>% 
      filter(level == level[names == .x]) 

    labels <- array(data = '',dim = nrow(filtered_grades))
    labels[filtered_grades$names == as.character(.x)] <- as.character(.x)

    p <- filtered_grades %>% 
      ggplot() +
      geom_col(aes(names, scores, fill = scores)) +
      scale_x_discrete(labels = labels)
      print(p)
  }) 

Created on 2018-05-17 by the reprex package (v0.2.0). reprex包 (v0.2.0)创建于2018-05-17。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM