Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading