Create a bar plot with 2 columns of a data frame in the x-axis in R

Viewed 26

I have a 2 columns of a data frame say

t1=data.frame(c1=c("Y","N","U","Y","N","U"),c2=c("Y","N","N","U","U","Y"))

I want to create a bar plot with both columns on the x axis and the number of occurrences of Y,N and U as 3 separate bars for each of the columns. Thank you!

1 Answers

Here is an option to achieve this with tidyverse:

Edit: This is probably what you wanted:

library(tidyverse)
t1 %>% pivot_longer(everything(), names_to = "name", values_to = "Let") %>%
  ggplot(aes(x=name, fill=Let))+
  geom_bar(position="dodge")

enter image description here

Previous post

library(tidyverse)
t1 %>% pivot_longer(everything(), names_to = "name", values_to = "Let") %>%
  ggplot(aes(x=Let, fill=name))+
  geom_bar()

enter image description here

Related