I have a dataframe that essentially looks like the following
score1 score2
1 1
2 2
3 3
4 4
5 5
They represent two different groups and I am trying to create a boxplot to compare them side by side. Is there a) a way of generating a boxplot or density plot with this data or b) a way of stacking them on top of each other to generate a dataframe that can be used to make such a plot? Here is the dataframe:
score1 <- c(1,2,3,4,5)
score2 <- c(1,2,3,4,5)
df <- data.frame(score1, score2)
Here's a tidyverse approach. Make the score labels and values into separate columns with pivot_longer
. Then overlay or facet plots for comparison.
Overlay
library(tidyverse)
df %>%
pivot_longer(everything()) %>%
ggplot(aes(value, color = name)) +
geom_density() # changed score2 to c(1,2,3,4,8) to show differences
df %>%
pivot_longer(everything()) %>%
ggplot(aes(value)) +
geom_density() +
facet_wrap(~name)
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.