Home > database >  Reordering a bar chart to be an ascending manner
Reordering a bar chart to be an ascending manner

Time:08-28

I have a bar chart presenting the number of samples, with the number of responders and non-responders in multiple datasets. I want to order it according to the number of samples in each dataset, in an ascending manner (starting with lowest samples number and going to the top).

the code:

myData <- data.frame(Articles, Samples_number, Response, No_Response)

library(ggplot2)

myData |>
  tidyr::pivot_longer(c(Response, No_Response)) |>
  ggplot(aes(Articles, value, fill = name))  
  geom_col(position = position_stack())  
  theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))

the plot:

enter image description here

The dataframe that contains all the information, myData :

structure(list(Articles = c("Allen.SKCM", "Auslander.SKCM", "Braun.KIRC", 
"Cho.NSCLC", "Freeman.SKCM (MGH)", "He.THCA", "Hoffman.BLCA (IMvigor210)", 
"Hugo.SKCM", "Liu.SKCM", "Lozano.SKCM", "McDermott.KIRC (IMmotion150)", 
"Motzer.KIRC", "Newell.SKCM", "Pender.PAN", "Riaz.SKCM", "Rizos.SKCM", 
"Snyder.BLCA"), Samples_number = c(49L, 181L, 44L, 14L, 354L, 
165L, 298L, 47L, 39L, 25L, 21L, 53L, 9L, 82L, 165L, 119L, 16L
), Response = c(10L, 57L, 22L, 2L, 197L, 48L, 68L, 27L, 7L, 13L, 
7L, 34L, 4L, 17L, 14L, 47L, 4L), No_Response = c(39L, 124L, 22L, 
12L, 157L, 117L, 230L, 20L, 32L, 12L, 14L, 19L, 5L, 65L, 24L, 
72L, 12L)), class = "data.frame", row.names = c(NA, -17L))

CodePudding user response:

use reorder function:

library(ggplot2)
myData |>
  tidyr::pivot_longer(c(Response, No_Response)) |>
  ggplot(aes(reorder(Articles, value), value, fill = name))  
  geom_col(position = position_stack())  
  theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))

enter image description here

CodePudding user response:

You can use the following code:

myData <- structure(list(Articles = c("Allen.SKCM", "Auslander.SKCM", "Braun.KIRC", 
                                      "Cho.NSCLC", "Freeman.SKCM (MGH)", "He.THCA", "Hoffman.BLCA (IMvigor210)", 
                                      "Hugo.SKCM", "Liu.SKCM", "Lozano.SKCM", "McDermott.KIRC (IMmotion150)", 
                                      "Motzer.KIRC", "Newell.SKCM", "Pender.PAN", "Riaz.SKCM", "Rizos.SKCM", 
                                      "Snyder.BLCA"), Samples_number = c(49L, 181L, 44L, 14L, 354L, 
                                                                         165L, 298L, 47L, 39L, 25L, 21L, 53L, 9L, 82L, 165L, 119L, 16L
                                      ), Response = c(10L, 57L, 22L, 2L, 197L, 48L, 68L, 27L, 7L, 13L, 
                                                      7L, 34L, 4L, 17L, 14L, 47L, 4L), No_Response = c(39L, 124L, 22L, 
                                                                                                       12L, 157L, 117L, 230L, 20L, 32L, 12L, 14L, 19L, 5L, 65L, 24L, 
                                                                                                       72L, 12L)), class = "data.frame", row.names = c(NA, -17L))

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)

myData %>%
  pivot_longer(c(Response, No_Response)) %>%
  group_by(Articles) %>%
  mutate(total = sum(value)) %>%
  ggplot(aes(x = Articles, y = value, fill = name))  
  geom_col(aes(x = reorder(Articles, total, sum), y = value), position = position_stack())  
  theme(axis.text.x = element_text(angle = 60, vjust = 1, hjust=1))

Created on 2022-08-28 with reprex v2.0.2

  • Related