I need to combine several variables into one graph (and that several times).
I found some code that directly pipes a long formart into ggplot. However, I would also like to change the names of these variables along the line.
library(ggplot2)
library(tidyverse)
Q1 <- c(1,4,5,3,2,4,3)
Q2 <- c(2,3,4,2,1,3,2)
Q3 <- c(2,3,4,2,4,4,3)
df <-data.frame(Q1,Q2,Q)
df %>%
pivot_longer(Q1:Q3, names_to = "question", values_to = "response") %>%
ggplot(aes(y = response, x = question)) +
geom_boxplot() +
labs(x = "Question", y = "Response")
This produces: actual image
But I want it to display specific variable lables instead of Q1 Q2 Q3, such as "This is question 1", "Another question", "Final question". How can I do that?