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.
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
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.