Search code examples
rggplot2histogramfrequency-table

Plot a histogram for a frequency table in R- ggplot2


I have a frequency table of number of occurrences of a variable per each value of reference (essentially bin size). For example (see data below): column 1, row 1 is reference value (0-5) and column 2, column 3 and column 4 represent the number of times status 1, status 2 and status 3 respectively were observed. I want to plot a histogram of number of occurrences of each status type for different reference values. Example, for a reference value of 5 on the x-axis, I want to see the occurrences of status 1, status 2 and status 3. Similarly for a reference value of 10, I want to see occurrences of status 1, status 2 and status 3. I am trouble figuring out how I should have my data frame setup to best answer this question. Any help is appreciated.

df_1 <- structure(list(ref = c(5L, 10L, 15L, 20L, 25L, 30L, 35L, 40L, 
45L, 50L, 55L, 60L, 65L, 70L, 75L, 80L, 85L, 90L, 95L, 100L, 
105L, 110L, 115L, 120L, 125L, 130L, 135L, 140L, 145L, 150L, 155L, 
160L, 165L, 170L, 175L, 180L, 185L, 190L, 195L, 200L), status1 = c(8341L, 
3045L, 788L, 39L, 8L, 8L, 6L, 12L, 21L, 18L, 12L, 15L, 9L, 4L, 
4L, 2L, 2L, 8L, 0L, 6L, 6L, 3L, 3L, 2L, 4L, 1L, 1L, 4L, 4L, 6L, 
1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L), status2 = c(0L, 537L, 
1345L, 1252L, 536L, 314L, 244L, 39L, 7L, 8L, 8L, 3L, 0L, 0L, 
3L, 2L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 
0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), status3 = c(0L, 0L, 
0L, 88L, 484L, 489L, 449L, 655L, 545L, 497L, 353L, 376L, 282L, 
261L, 332L, 241L, 182L, 140L, 166L, 148L, 143L, 109L, 89L, 74L, 
39L, 85L, 60L, 39L, 26L, 31L, 20L, 10L, 2L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L)), row.names = c(NA, -40L), class = "data.frame")

Solution

  • First make your data from wide to longer using pivot_longer. After that you can create a stacked barplot like this:

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    df_1 %>%
      pivot_longer(-ref) %>%
      ggplot(aes(x = ref, y =value, fill = name)) +
      geom_bar(stat = "identity", position = "stack")
    

    Output:

    enter image description here