How to create a grouped barplot using ggplot?

Load packages

library(tidyverse)
library(janitor)


```{r}
economics<-economics %>% 
filter(date>"1967-07-01"|date<"1968-05-01")

ggplot(economics)+geom_bar(mapping=aes(date,unemploy),stat = "identity",position="dodge")+geom_bar(mapping=aes(date,pce),stat ="identity",position="dodge")

Hi,

In general it would help if you can post a reprex (FAQ: How to do a minimal reproducible example ( reprex ) for beginners) so we can work with your data and just a description of what exactly you want to accomplish.

Here is an example of how you can create a grouped barplot:

# library
library(ggplot2)

# create a dataset
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
data <- data.frame(specie,condition,value)

# Grouped
ggplot(data, aes(fill=condition, y=value, x=specie)) + 
  geom_bar(position="dodge", stat="identity")

Created on 2021-10-26 by the reprex package (v2.0.0)

The code below does not provide a grouped bar chart.

library(tidyverse)
library(janitor)
#> 
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#> 
#>     chisq.test, fisher.test
nithin<-tibble::tribble(
  ~Student, ~cog_score, ~lang_score, ~num_score,
        1L,        33L,         26L,        44L,
        2L,        36L,         36L,        23L,
        3L,        24L,         23L,        29L,
        4L,        22L,         32L,        24L,
        5L,        27L,         43L,        40L,
        6L,        31L,         50L,        38L,
        7L,        37L,         21L,        29L,
        8L,        21L,         42L,        47L,
        9L,        26L,         28L,        29L,
       10L,        36L,         32L,        38L
  )
head(nithin)
#> # A tibble: 6 x 4
#>   Student cog_score lang_score num_score
#>     <int>     <int>      <int>     <int>
#> 1       1        33         26        44
#> 2       2        36         36        23
#> 3       3        24         23        29
#> 4       4        22         32        24
#> 5       5        27         43        40
#> 6       6        31         50        38
ggplot(nithin)+geom_bar(mapping=aes(Student,cog_score),stat="identity",position="dodge")+geom_bar(mapping=aes(Student,lang_score),stat="identity",position="dodge")+geom_bar(mapping=aes(Student,num_score),stat="identity",position="dodge")

Created on 2021-10-26 by the reprex package (v2.0.1)

You haven't said (and there doesnt seem to be a good choice of variable) on which to group on...

Once you have suitable data on which to group: the fill aesthetic will assign different fill colours to bars thereby giving a 'grouped' appearance.
you can study how to use ggplot here
https://r4ds.had.co.nz/

1 Like

Yes I have gone through this book. The problem is precisely on creating a new variable. My original data has details of about 9000+ students. Is there any other way to get around this problem? Outside the ggplot package??

ok, on what principle would you wish to group the students ?
You should describe how you wish to group them, so that we can help you to group them in that way.

ok, so i will explain the context. The data is the academic score of students. The questionnaire had 3 sections - Cognitive, Language and Numeracy sections. In the bar chart I want the top 10 or 15 students (on x-axis) and their scores of each section (on y-axis).

In excel, it comes like this. This is how I want it.

image

Have you identified the top 15 students ?
Do you have a criteria for doing that?
if someone scored 20,25,30 are they better or worse than 25,25,25 ?

Once you find the students to plot, you can simply do:


library(tidyverse)

nithin<-tibble::tribble(
  ~Student, ~cog_score, ~lang_score, ~num_score,
  1L,        33L,         26L,        44L,
  2L,        36L,         36L,        23L,
  3L,        24L,         23L,        29L,
  4L,        22L,         32L,        24L,
  5L,        27L,         43L,        40L,
  6L,        31L,         50L,        38L,
  7L,        37L,         21L,        29L,
  8L,        21L,         42L,        47L,
  9L,        26L,         28L,        29L,
  10L,        36L,         32L,        38L
) %>% mutate(Student=factor(Student))


(nithin_long <- pivot_longer(data=nithin,
                            cols = -Student))
ggplot(nithin_long)+
  geom_col(mapping=aes(Student,
                       y=value,
                       fill=name),
           position="dodge")

yeah we have taken students with more than 25 marks in cognitive section.

I am getting the barchart as desired. Thanks a lot for this.

I am facing an issue with the data labels here. I will add the screenshot and the code used.

ggplot(section_long)+geom_bar(mapping=aes(level,score,fill=section),stat="identity",position="dodge")+geom_text(aes(level,score,label=score))

referring to the earlier nithin_long data

ggplot(nithin_long) +
  geom_col(
    mapping = aes(Student,
      y = value,
      fill = name
    ),
    position = "dodge"
  ) +
  geom_text(aes(
    x = Student,
    y = value + 1,
    group = name,
    label = value
  ),
  position = position_dodge(width = 1)
  )

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.