Search code examples
rggplot2labelstacked-bar-chart

Change labels of name and value columns using pivot longer


Hello I am new to R and I am trying to make a stacking bar plot with each bar corresponding to a different year using ggplot. Year will be the x axis while Frequency is on the y axis. I can make the stacked bar plot just with "values" and "name". But I do not understand how to change the labels from value and name to my desired ones using pivot-longer (frequency instead of values and serotypes instead of name). Please let me know how to do this. Thank you.

My stacked bar plot

library(dplyr)
library(tidyr)
library(ggplot2)

data2 <- data.frame(
  Type12 = c(0,0,0,0,1,0,0,0), 
  Type15 = c(0,1,1,0,0,0,0,0), 
  Type28 = c(1,2,0,0,2,2,2,0),
  Type46 = c(1,0,0,0,0,1,0,0),
  Type50 = c(0,0,0,0,1,1,2,2),
  Type52 = c(0,0,0,0,0,1,0,0),
  Type62 = c(0,1,0,0,0,0,0,0),
  Type63 = c(0,0,0,0,0,0,1,1),
  Type75 = c(0,0,0,0,0,0,0,1),
  Type101 = c(0,0,1,0,1,0,0,0),
  Type120 = c(0,0,0,0,1,2,2,3),
  Type167 = c(0,1,0,0,0,0,0,0),
  Type172 = c(0,1,1,3,0,0,0,1),
  Type182 = c(1,0,1,0,0,0,0,0),
  Type188 = c(0,0,0,1,0,0,0,0),
  Type334 = c(1,0,0,0,0,0,0,0),
  Type347 = c(0,0,0,0,1,0,0,0),
  Type382 = c(1,0,0,0,0,0,0,0),
  Type404 = c(0,1,4,3,0,0,0,0),
  Type433 = c(0,0,0,0,0,2,4,10),
  Type579 = c(1,0,1,0,0,0,0,0),
  Type853 = c(0,0,0,0,0,1,0,0),
  Type909 = c(0,0,0,0,1,3,3,0),
  Type1122 = c(0,0,1,0,0,0,0,0),
  Unknown = c(1,0,0,0,0,0,1,0),
  Year = as.factor(c("2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021"))

)
 
data2 %>% 
  pivot_longer(-Year) %>% 
  ggplot(aes(x = Year, y = value, fill = name)) + 
  geom_col(position = "stacked")

Solution

  • By default pivot_longer creates a new column containing the names which is named name and a new column containing the values which is named value.

    But you could set your desired names using the names_to and values_to arguments, i.e. in your case do

    pivot_longer(-Year, names_to = "serotypes", values_to = "frequency")
    
    data2 %>% 
      pivot_longer(-Year, names_to = "serotypes", values_to = "frequency") %>% 
      ggplot(aes(x = Year, y = frequency, fill = serotypes)) + 
      geom_col(position = "stack")
    

    enter image description here