Home > other >  geom bar for multiplay variables (columns) in ggplot
geom bar for multiplay variables (columns) in ggplot

Time:06-11

I have a data set with multiplay variables which referes to sympthoms people have (0 exist,1 is not exist)

I would like to put all of them in geom bar on x axis, and the y axis will be the count of them in my dataset.

 ggplot(tested_individuals, aes(x=Cough))   
      geom_bar(stat = "count")

CodePudding user response:

library(tidyverse)

data <- tribble(
  ~Cough, ~Fever, ~Head_ache,
  0, 1, 1,
  0, 1, 0,
  1, 1, 1
)

data %>%
  pivot_longer(everything()) %>%
  group_by(name) %>%
  summarise(value = sum(value)) %>%
  ggplot(aes(name, value))  
  geom_col()

Created on 2022-06-10 by the enter image description here

Created on 2022-06-10 by the reprex package (v2.0.1)


Data used - transcribed from image in question

df <- structure(list(Cough = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 
0, 0), Fever = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0), Sore_throat = c(0, 
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), Shortness_of_breath = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Head_ache = c(0, 0, 0, 
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), Corona_result = c("Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive", "Positive", "Positive", "Positive", "Positive", "Positive", 
"Positive"), Age_60_and_above = c("No", "No", "No", "No", "No", 
"No", "No", "No", "Yes", "No", "No", "No", "No", "No"), 
Test_indication = c("Other", 
"Other", "Other", "Other", "Abroad", "Abroad", "Other", "Other", 
"Contact with confirmed", "Other", "Other", "Other", "Other", 
"Other")), row.names = c(NA, -14L), class = "data.frame")
  • Related