Create barplot based on part of outcomes

24

Based on this outcomes (means <-aggregate(subset1[vars2], by=list(name1=subset1$Leerjaar), mean)), I would like to create a barplot for each school, so a barplot for school 1 with the mean scores for variable 1 and 2, a barplot for school 2 with the mean scores for variable 1 and 2, and so on. Is it possible to create this barplots in Rstudio automatically for every school, based on this outcomes?

Yes, it is, here is an example of how to do it, using some built-in dataset since you haven't provided sample data on an adequate format (screenshots are not very useful).

library(tidyverse)

# Similar made up sample data since you have not provide it on an adequate format
sample_data <- iris %>% 
    group_by(Species) %>% 
    summarise(mean_length = mean(Sepal.Length),
              mean_width = mean(Sepal.Width))
sample_data
#> # A tibble: 3 x 3
#>   Species    mean_length mean_width
#>   <fct>            <dbl>      <dbl>
#> 1 setosa            5.01       3.43
#> 2 versicolor        5.94       2.77
#> 3 virginica         6.59       2.97

# Generating the column plots
sample_data %>%
    gather(Feature, Value, -Species) %>% 
    ggplot(aes(x = Feature, y = Value, , fill = Feature)) +
    geom_col(show.legend = FALSE) +
    facet_wrap("Species") + 
    theme_light() +
    theme(axis.text.x = element_text(angle=30, hjust=1, vjust=1))

Created on 2019-09-03 by the reprex package (v0.3.0.9000)

2 Likes

Thank you very much!
I will keep in mind to provide a sample data on an adequate format for next time.

I have one other question. Creating this barplot works very well. However, I was wondering if it is possible to split the barplot, so that I have one barplot for every school (with a legend for every school, a x-as and y-as, ...). I used the function "lapply(split....))", as you can see below, but it doesn't work.

Thank you in advance.


##Used dataset
Example_Data <- tribble(
  ~Student,    ~School, ~AUT_RM_School, ~Contr_RM_School, ~Aut_RM_Home, ~Contr_RM_Home,
  "Studen 1", "School 1",            2.34,              2.89,          1.63,            2.22,
  "Studen 2", "School 1",            2.63,              1.89,          3.75,            1.11,
  "Studen 3", "School 2",            2.63,              1.56,          2.50,            1.33,
  "Studen 4", "School 2",            1.38,              1.56,          4.13,            2.00,
  "Studen 5", "School 2",            3.25,              1.78,          4.13,            1.33,
  "Studen 6", "School 3",            4.50,              2.00,          4.00,            1.44,
  "Studen 7", "School 3",            3.63,              2.67,          4.00,            1.44,
  "Studen 8", "School 3",            2.50,              2.22,          4.00,            1.11,
  "Studen 9", "School 3",            4.38,              2.67,          4.13,            1.56,
  "Studen 10", "School 4",            4.00,              2.67,          4.38,            2.11,
  "Studen 11", "School 4",            3.75,              1.89,          4.75,            2.00,
  "Studen 12", "School 4",            4.13,              2.11,          4.75,            1.56,
  "Studen 13", "School 4",            4.13,              2.56,          2.25,            3.22
)
##Calculate mean per school for different variables (AUT_RM_School, Contr_RM_School, Aut_RM_Home, Contr_RM_Home,)
vars_example<- c("AUT_RM_School", "Contr_RM_School", "Aut_RM_Home", "Contr_RM_Home")
summary(Example_Data[vars_example])
aggregate(Example_Data[vars_example], by=list(name1=Example_Data$School), mean, na.rm = TRUE)


Sample_Data <- Example_Data %>% 
  group_by(School) %>% 
  summarise(Autonomous_ReadingMotivation_School = mean(AUT_RM_School, na.rm = TRUE),
            Controlled_ReadingMotivation_School = mean(Contr_RM_School, na.rm = TRUE),
            Autonomous_ReadingMotivation_Home = mean(Aut_RM_Home, na.rm = TRUE),
            Controlled_ReadingMotivation_Home = mean(Contr_RM_Home, na.rm = TRUE))

# Generating the column plots and split bar plots
lapply(split(DatasetLLN_Rstudio, DatasetLLN_Rstudio$School), function(x){
Sample_Data %>%
  gather(Feature, Value, -School) %>% 
  ggplot(aes(x = Feature, y = Value, , fill = Feature)) +
  geom_col(show.legend = TRUE) +
  facet_wrap("School") + 
  theme_light() +
  theme(axis.text.x = element_text(angle=20, hjust=1, vjust=1))
}

##Error: unexpected symbol in:
"}
lapply"

To help us help you, could you please turn this into a proper reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

I apologize for the inconvenience.

I adjusted my question. Can I leave the dataset as follows or does this still need to be shortened? However, I find no further information on the specified page about how this kind of dataset should be further shortened.

I'm still not sure if I understand what you are trying to do (also your example is still not reproducible since you are not including library calls as explained in the reprex guide) but I think you are trying to do something like this.

library(tidyverse)

Sample_Data <- data.frame(stringsAsFactors=FALSE,
                          School = c("School 1", "School 2", "School 3", "School 4"),
                          Autonomous_ReadingMotivation_School = c(2.485, 2.42, 3.7525, 4.0025),
                          Controlled_ReadingMotivation_School = c(2.39, 1.63333333333333, 2.39, 2.3075),
                          Autonomous_ReadingMotivation_Home = c(2.69, 3.58666666666667, 4.0325, 4.0325),
                          Controlled_ReadingMotivation_Home = c(1.665, 1.55333333333333, 1.3875, 2.2225)
)

Sample_Data %>%
    gather(Feature, Value, -School) %>%
    group_nest(School) %>% 
    mutate(plot = map2(data, School, ~{ggplot(.x, aes(x = Feature, y = Value, fill = Feature)) +
            geom_col(show.legend = TRUE) +
            theme_light() +
            labs(title = paste0(.y)) +
            theme(axis.text.x = element_text(angle=20, hjust=1, vjust=1))
    })) %>% 
    pull(plot)
#> [[1]]

#> 
#> [[2]]

#> 
#> [[3]]

#> 
#> [[4]]

1 Like

Indeed, this was my question, thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.