Making barplots side by side for more than one column

I have the following data:

structure(list(seed_num = c(1, 2, 3, 4, 5), overall_dots = c(49L, 
49L, 49L, 49L, 49L), Lower_right = c(34L, 24L, 15L, 24L, 19L), 
    SP = c(14L, 11L, 4L, 9L, 8L), SC = c(4L, 4L, 5L, 5L, 5L)), class = "data.frame", row.names = c(NA, 
-5L))

I want to make a bar plot, something that looks like this:

enter image description here

The X axis would be the seed number, and for each seed I want to present the other 4 columns. So I have in this data 5 seeds, each seed has 4 columns, hence 4 bars. How can I do that ?

>Solution :

You have to reshape your data to long or tidy format. Afterwards plotting is pretty straightforward:

library(tidyverse)

dat |> 
  pivot_longer(-seed_num, names_to = "column") |> 
  mutate(column = forcats::fct_inorder(column)) |> 
  ggplot(aes(seed_num, value, fill = column)) +
  geom_col(position = "dodge") +
  theme(legend.position = "bottom")

Leave a Reply