0

I would like to create a barplot in r that takes two y variables and displays them side by side, for one x variable. Below is the output of a put function of the data that I am trying to plot

structure(list(SpindleID = structure(1:20, .Label = c("3001", 
"3002", "3003", "3004", "3005", "3006", "3007", "3008", "3009", 
"3010", "3501", "3502", "3503", "3504", "3505", "3506", "3507", 
"3508", "3509", "3510"), class = "factor"), lowtor = c(39, 83, 
70, 123, 31, 36, 128, 213, 134, 27, 223, 197, 286, 65, 744, 664, 
465, 60, 140, 115), hitor = c(0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 5, 
8, 1, 0, 155, 96, 28, 27, 12, 16)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -20L))

The x variable is SpindleID and the two y variables are lower and hitor.

Thanks

1
  • Actually just barplot(t(df[-1]), beside=TRUE). Commented Aug 20, 2022 at 12:43

1 Answer 1

1

First, make a longer format dataframe using pivot_longer to make it easy to show your two variables as bars side by side with ggplot like this:

library(ggplot2)
library(dplyr)
library(tidyr)
df %>%
  pivot_longer(cols = -SpindleID) %>%
  ggplot(aes(x = SpindleID, y = value, fill = name)) +
  geom_bar(stat = "identity", position = "dodge") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Created on 2022-08-20 with reprex v2.0.2

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.