Home > front end >  How to customise major grid line for bar chart in ggplot?
How to customise major grid line for bar chart in ggplot?

Time:06-02

Can someone explain to me why the following code isn't working to change major gridlines on the y-axis? I want it to go from 0 to 20 with major lines every 2 units. The code doesn't return any errors.

Grade <- c(rep("6th" , 12) , rep("7th" , 13) , rep("8th" , 16) , rep("9th" , 9), 
           rep("10th" , 10) , rep("11th" , 6), rep("12th" , 3))
Gender <- c(rep("Male" , 5), rep("Female" , 7), rep("Nonbinary" , 0),
            rep("Male" , 5), rep("Female" , 8), rep("Nonbinary" , 0),
            rep("Male" , 7), rep("Female" , 8), rep("Nonbinary" , 1),
            rep("Male" , 4), rep("Female" , 4), rep("Nonbinary" , 1),
            rep("Male" , 6), rep("Female" , 4), rep("Nonbinary" , 0),
            rep("Male" , 2), rep("Female" , 4), rep("Nonbinary" , 0),
            rep("Male" , 0), rep("Female" , 3), rep("Nonbinary" , 0))
value <- rep("1", 69)
datas <- data.frame(Grade,Gender,value)
datas <- datas %>% arrange(desc(Grade)) %>% mutate(rank = c(rep("4" , 9) , rep("3" , 16) , 
                                 rep("2" , 13) , rep("1" , 12), 
                                 rep("7" , 3) , rep("6" , 6), 
                                 rep("5" , 10))) %>% arrange(desc(rank))
ggplot(datas, aes(x = rank, y = value, fill = Gender))  
      geom_col(position = position_stack(reverse = TRUE))   
      scale_x_discrete(labels=c("1" = "6th", "2" = "7th","3" = "8th","4" = "9th", 
                                "5" = "10th", "6" = "11th", "7" = "12th"))   
      scale_y_discrete(breaks = seq(0,20,2))   
      xlab('Grade')   ylab('')

This is the graph it returns.

CodePudding user response:

Your problem is caused by value being a character column rather than numeric. This makes the y axis categorical rather than numeric, so your breaks are ignored because there are no categories other than "1" on the y axis at which to put the breaks.

Since the values are all meant to be 1, you can use geom_bar which will make the y axis into counts. You can then add breaks with scale_y_continuous rather than scale_y_discrete

ggplot(datas, aes(x = rank, fill = Gender))  
      geom_bar(position = position_stack(reverse = TRUE))   
      scale_x_discrete(labels=c("1" = "6th", "2" = "7th","3" = "8th","4" = "9th", 
                                "5" = "10th", "6" = "11th", "7" = "12th"))   
      scale_y_continuous(breaks = seq(0,20,2))   
      xlab('Grade')   ylab('')

enter image description here

If you want to interpret value as numbers (in case any of them are not 1), then use geom_col, but convert value to be numeric rather than character:

ggplot(datas, aes(x = rank, y = as.numeric(value), fill = Gender))  
      geom_col(position = position_stack(reverse = TRUE))   
      scale_x_discrete(labels=c("1" = "6th", "2" = "7th","3" = "8th","4" = "9th", 
                                "5" = "10th", "6" = "11th", "7" = "12th"))   
      scale_y_continuous(breaks = seq(0,20,2))   
      xlab('Grade')   ylab('')

enter image description here

  • Related