rggplot2geom-barjanitor

Can I make a bar plot, where each bar represents a column in a data frame?


I would like to make a bar plot, where each bar is represented by one of the three columns in this data frame. The 'size' of each bar depends on the sum created by adorn_totals.

Reproducible example:

library(janitor)

test_df <- data.frame(
  a = c(1:5),
  b = c(1:5),
  c = c(1:5)
  ) %>% 
  adorn_totals(where = 'row', tabyl = c(a, b, c))

I tried a solution that has previously been posted, but that didn't work: Link to the post: Bar plot for each column in a data frame in R

library(janitor)
library(ggplot2)

df <- data.frame(
  a = c(1:5),
  b = c(1:5),
  c = c(1:5)
  ) %>% 
  adorn_totals(where = 'row', tabyl = c(a, b, c))

lapply(names(df), function(col) {
  ggplot(df, aes(.data[[col]], ..count..)) + 
    geom_bar(aes(fill = .data[[col]]), position = "dodge")
}) -> list_plots

Solution

  • This is one way:

    library(janitor)
    library(ggplot2)
    
    test_df <- data.frame(
      a = c(1:5),
      b = c(1:5),
      c = c(1:5)
      ) %>% 
      adorn_totals(where = 'row', tabyl = c(a, b, c))
    
    tail(test_df,1) %>% stack() %>% 
      ggplot(aes(ind, values)) + geom_col()
    

    Created on 2022-11-07 with reprex v2.0.2

    Of course, you don't need to totalize the df before plotting it, since ggplot does it for you. I add another example with an explanation of stack, some color, and no totals.

    library(ggplot2)
    
    test_df <- data.frame(
      a = c(1:5),
      b = c(1:5),
      c = c(1:5))
    
    test_df |> stack()
    #>    values ind
    #> 1       1   a
    #> 2       2   a
    #> 3       3   a
    #> 4       4   a
    #> 5       5   a
    #> 6       1   b
    #> 7       2   b
    #> 8       3   b
    #> 9       4   b
    #> 10      5   b
    #> 11      1   c
    #> 12      2   c
    #> 13      3   c
    #> 14      4   c
    #> 15      5   c
    
    test_df |> stack() |> 
      ggplot(aes(ind, values, fill=ind)) + geom_col()
    

    Created on 2022-11-07 with reprex v2.0.2