I have a dataframe that includes a number of variables from a survey in which each variable is a statement that respondents could either agree with (Y) or disagree with (N). I would ideally like to create a bar chart that includes all of these variables on the x axis, with the y axis as the count of people who agreed. Which is the other twist - I don't want to chart those who disagreed; only those who agreed. I think I can figure this part out - I'm assuming changing the Ns to NAs - but any guidance is appreciated.
But from what I've read of ggplot2, it's not made to handle more than one variable on the x axis, and the only solution I've seen for others is to do a long pivot, but those situations don't seem to fit what I'm trying to do.
So with the below example, the bar chart would ideally include three bars on the x axis, with the first bar representing all the Ys for Statement1, the second representing all the Ys for Statement2, and the third bar would be all the Ys for Statement 3.
You should pivot the data to a long format where there is one variable marking which statement is relevant and another column showing Y or N. You can then filter to keep only Y values. Finally, geom_bar() will count how many times each statement appears.
library(dplyr)
library(tidyr)
library(ggplot2)
df <- data.frame(
Statement1 = c("Y", "Y", "N", "N", "Y"),
Statement2 = c("Y", "N", "Y", "N", "N" ),
Statement3 = c("N", "Y", "N", "Y", "Y")
)
df
#> Statement1 Statement2 Statement3
#> 1 Y Y N
#> 2 Y N Y
#> 3 N Y N
#> 4 N N Y
#> 5 Y N Y
df |> pivot_longer(cols = everything(), names_to = "Statement", values_to = "Value") |>
filter(Value == "Y") |>
ggplot(aes(x = Statement)) + geom_bar()
The approach suggested by the @FJCC is indeed the right way to create a bar chart with multiple variables on the x-axis, where only the "Y" values are considered.
By pivoting the data to a long format using 'pivot_linger()' and filtering out the "N" values, you can create a data frame that represent the desired chart. Then, using ggplot2 and geom_bar(), you can plot the counts of "Y" values for each statement.
This code will generate a bar chart with three bars on the x-axis, representing the counts of "Y" values for Statement1, Statement2, and Statement3, respectively.