Adding one label to each bar graph r

拈花ヽ惹草 提交于 2019-12-10 07:44:38

问题


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)


回答1:


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]])




回答2:


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:

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).



来源:https://stackoverflow.com/questions/50393570/adding-one-label-to-each-bar-graph-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!