0

My input:

foo <- c(5,17,31,9,17,10,30)
bar <- c(28,16,29,14,34,6,18)
day <- c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Suday")
df <- data.frame(foo, bar, day)

I trying combine two bar plots:

ggplot(df, aes(x=day, y=foo)) +  geom_col() +coord_flip()
ggplot(df, aes(x=day, y=bar)) +  geom_col() +coord_flip()

I want present in one plot side by side columns from two continuous variables(foo and bar) in my y-axis and one discrete variable day in my x-axis. So for y-axis use foo and bar and for x-axis use day

Is it possible?

TeoK
  • 511
  • 6
  • 13

1 Answers1

1

Bring your data in long format with pivot_longer:

library(tidyverse)
df %>% 
  pivot_longer(
    cols = -day
  ) %>% 
  ggplot(aes(x=day, y=value, fill=name)) +
  geom_col(position = "dodge")+
  coord_flip()

enter image description here

or without coord_flip

df %>% 
  pivot_longer(
    cols = -day
  ) %>% 
  ggplot(aes(x=day, y=value, fill=name)) +
  geom_col(position = "dodge")

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66