[英]R: Two columns and trying to make a density or boxplot. How do I stack them
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?
是否有 a) 一种使用这些数据生成箱线图或密度图的方法,或者 b) 一种将它们堆叠在一起以生成可用于制作此类图的数据框的方法? 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.这是一个tidyverse方法。 Make the score labels and values into separate columns with
pivot_longer
.使用
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.