Home > Enterprise >  How do I adjust my tibble to get a grouped bar chart in ggplot2?
How do I adjust my tibble to get a grouped bar chart in ggplot2?

Time:11-24

I think the code itself I'm using for a grouped barchart is roughly correct. However, my tibble doesn't have a way to call the three categories I need (Views, Interactions, and Comments). I have a conceptual issue in making this work.

This is what I'm trying to execute in ggplot2:

bp_vic <- ggplot(data, aes(x = Day, y = value, fill = category))  
  geom_bar(position = 'dodge', stat = 'identity')
bp_vic

Value and fill may be off. However, I think the main issue is not having a proper category call.

My tibble has six columns. The last three are what I'm trying to plot. Integer counts for Views, Interactions, and Comments.

enter image description here

Or you could easily plot the Category rather than the day if needed too, by having x = Category instead of x = Day. enter image description here

If you would like to change the order of the categories, then you can make Category a factor, which you can do without changing the dataframe.

# Create order for the categories. If you want to do it by the number of views, then you can create the list from your dataframe.
level_order <- data %>%
  dplyr::filter(Section == "Views") %>%
  dplyr::arrange(desc(values)) %>%
  pull(Category) %>%
  unique()
  
# Then, set category as a factor and include the ordered categories.
ggplot(data, aes(fill = Section, y = values, x = factor(Category, level = level_order)))  
  geom_bar(position = "dodge", stat = "identity")

enter image description here

  • Related