0

My data:
My data

I have a data set containing presence of various chemicals across multiple waterbodies. I want to create a barplot of the frequency of "Yes" values across multiple variables/columns.

I've tried various attempts using base r and ggplot2, the problem I am consistently running into is that the functions want the sub variable id or just a singular variable and I am looking to select the range of chem1 through chem5 to display.

ggplot(MyData, aes(x=3:7, y="Yes")) + geom_bar()

Created on 2022-02-22 by the reprex package (v2.0.1)

I know this does not work but I wanted to attach to delineate my goal in r. The 3:7 is the range of columns I want plotted.

I am new to R and sifted through similar questions and couldn't figure it out.

  • 1
    Please consider adding data to your questions rather than posting an image. You can do that using `dput(df)`. – Vishal A. Feb 22 '22 at 06:14
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please don't post pictures of data; we cannot copy/paste those values in R for testing. The main problem seems to be your data is not in a "tidy" format that `ggplot` expected it to be. Different bars should correspond to different rows, not columns. `pivot_longer` can help. – MrFlick Feb 22 '22 at 06:32
  • Please provide enough code so others can better understand or reproduce the problem. – Community Feb 26 '22 at 20:08

1 Answers1

0

For plotting with ggplot2, the data should be in a long format. Once it is in long format, then we can just keep the Yes values, and use count with geom_bar.

library(tidyverse)

df %>% 
  pivot_longer(everything()) %>% 
  filter(value == "Yes") %>% 
  ggplot(aes(name)) + 
  geom_bar(stat = "count")

Output

enter image description here

Data

df <- structure(list(chem1 = c("Yes", "Yes", "Yes", "No", "Yes", "No", 
"Yes", "No", "Yes", "No"), chem2 = c("No", "Yes", "No", "Yes", 
"No", "Yes", "No", "Yes", "No", "Yes")), class = "data.frame", row.names = c(NA, 
-10L))
AndrewGB
  • 16,126
  • 5
  • 18
  • 49