【发布时间】:2021-08-09 17:41:26
【问题描述】:
我想制作一个箱线图如下: x 轴上:不同的组(健康、疾病 1、疾病 2) 在y轴上:大脑大小,以不同颜色并排显示“左脑大小”和“右脑大小”
我正在使用 ggplot 函数
data <- df[c("Group", "Left brain size", "Right brain size")]
ggplot(data, aes(x=Group ,y=..))+
geom_boxplot()
我应该如何组织我的数据:x = Group,y = Brain size,fill = side?p>
谢谢! PS:我的数据示例如下表
| Group | Left brain size | Right brain size |
|---|---|---|
| Healthy | 0.5 | 0.9 |
| Healthy | 0.4 | 0.8 |
| Healthy | 0.8 | 0.4 |
| Disease 1 | 0.7 | 0.5 |
| Disease 1 | 0.9 | 0.3 |
| Disease 1 | 0.2 | 0.1 |
| Disease 2 | 0.3 | 0.8 |
| Disease 2 | 0.4 | 0.54 |
| Disease 2 | 0.1 | 0.4 |
| Disease 2 | 0.3 | 0.2 |
【问题讨论】:
-
您好,您可以使用
tidyr::pivot_longer()来重塑您的数据以获得列“大脑大小”。它应该看起来像:tidyr::pivot_longer(df, cols = c("Left brain size", "Right brain size"), names_to = "side", values_to = "size") -
然后,像这样使用这个新的df:
ggplot(data = df, aes(x = Group, y = size, fill = side)) + geom_...。 -
这能回答你的问题吗? Reshaping data to plot in R using ggplot2