使用 ggplot 和 R 绘制三层嵌套数据
Plotting Three Levels of Nested Data Using ggplot and R
我最近 运行 一些 2x3 混合方差分析用于一个神经科学项目,年龄和效价作为相关值的预测因子。这些分析 运行 三次,每个 ROI 或大脑区域一次。我可以创建点线图来可视化每个 ROI 的数据,几乎没有问题:
#Dataframe creation and data import
df <- data.frame(matrix(NA, nrow = 6,
ncol = 1,
dimnames = list(1:6, "Age")))
df$Age <-c(rep("Child", 3), rep("Adult", 3))
df$Valence <- c(rep(c("Negative", "Mixed", "Positive"),2))
df$Cor <- c(0.319, 0.303, 0.278, 0.200, 0.230, 0.216)
df$ci <- c(0.0648, 0.0548, 0.0557, 0.0631, 0.0542, 0.0649)
#Plotting
ggplot(df, aes(x = Age, y = Cor, group = Valence, color = Valence)) +
stat_summary(geom="pointrange", position=position_dodge(width = .2), fun.data = NULL) +
stat_summary(geom="line", position=position_dodge(width = .2), fun.data = NULL) +
geom_errorbar(aes(ymin = Cor - ci, ymax = Cor + ci), size= 0.3, width=0.2, position=position_dodge(width = .2)) +
scale_fill_hue(name="Valence Type", breaks=c("Negative", "Mixed", "Positive"), labels=c("Negative", "Mixed", "Positive")) +
scale_x_discrete(name = "Age Groups",
labels = c("Child", "Adult")) +
scale_y_continuous(limits=c(0.10,0.40)) +
ggtitle("Representational Similarity By Age and Valence") +
labs(y="Mean Correlation Value") +
theme_bw() +
theme(title = element_text(face="bold", size=14)) +
theme(axis.text.x = element_text(size=14, color = "Black")) +
theme(axis.text.y = element_text(size=14, color = "Black"))
但是,我想做的是将三个独立的绘图合并为一个,以便所有三个 ROI 的结果都显示在同一个图像中;类似于下图,其中每个 ROI 的结果并排显示:
MS Paint Rendering of my Scripting Inadequacies
不过,我没能找到解决此问题的脚本解决方案。我想也许我可以尝试嵌套 X 轴值,因为那是我在概念上所做的,当然那是行不通的。我尝试结合 ROI 和年龄因素,这样我就有了“Child, AMY”、“Adult, AMY”、“Child, NAcc”等,但是使用点线格式,这些线延伸到 ROI,这是不理想。由于对阅读条形图时存在的偏差的研究,点线是我在可视化方面的偏好,但如果有人使用条形图对此有很好的解决方案,我不会反对它。
我在下面包含了一个摘要数据框和我的样板 ggplot 脚本。非常感谢任何帮助,即使它只是告诉我我正在尝试做的事情是不可能的!
#Dataframe creation and data import
df <- data.frame(matrix(NA, nrow = 18,
ncol = 1,
dimnames = list(1:18, "Age")))
df$Age <-c(rep("Child", 9), rep("Adult", 9))
df$Valence <- c(rep(c(rep("Negative",3), rep("Mixed",3), rep("Positive",3)),2))
df$ROI <- rep(c("AMY", "NAcc", "vmPFC"), 6)
df$Cor <- c(0.229, 0.268, 0.319, 0.236, 0.260, 0.303, 0.213, 0.236, 0.278, 0.188, 0.205, 0.200, 0.221, 0.226, 0.230, 0.208, 0.210, 0.216)
df$ci <- c(0.0474, 0.0675, 0.0648, 0.0445, 0.0456, 0.0548, 0.0531, 0.0518, 0.0557, 0.0485, 0.0727, 0.0631, 0.0336, 0.0541, 0.0542, 0.0430, 0.0604, 0.0649)
#Plotting
ggplot(df, aes(x = Age, y = Cor, group = Valence, color = Valence)) +
stat_summary(geom="pointrange", position=position_dodge(width = .2), fun.data = NULL) +
stat_summary(geom="line", position=position_dodge(width = .2), fun.data = NULL) +
geom_errorbar(aes(ymin = Cor - ci, ymax = Cor + ci), size= 0.3, width=0.2, position=position_dodge(width = .2)) +
scale_fill_hue(name="Valence Type", breaks=c("Negative", "Mixed", "Positive"), labels=c("Negative", "Mixed", "Positive")) +
scale_x_discrete(name = "Age Groups",
labels = c("Child", "Adult")) +
scale_y_continuous(limits=c(0.10,0.40)) +
ggtitle("Representational Similarity By Age and Valence") +
labs(y="Mean Correlation Value") +
theme_bw() +
theme(title = element_text(face="bold", size=14)) +
theme(axis.text.x = element_text(size=14, color = "Black")) +
theme(axis.text.y = element_text(size=14, color = "Black"))
你可以试试这个(你必须包装图并调整条带的一些细节)。使用的数据是 df
(嵌套)的最新版本:
library(tidyverse)
#Data
df <- data.frame(matrix(NA, nrow = 18,
ncol = 1,
dimnames = list(1:18, "Age")))
df$Age <-c(rep("Child", 9), rep("Adult", 9))
df$Valence <- c(rep(c(rep("Negative",3), rep("Mixed",3), rep("Positive",3)),2))
df$ROI <- rep(c("AMY", "NAcc", "vmPFC"), 6)
df$Cor <- c(0.229, 0.268, 0.319, 0.236, 0.260, 0.303, 0.213, 0.236, 0.278, 0.188, 0.205, 0.200, 0.221, 0.226, 0.230, 0.208, 0.210, 0.216)
df$ci <- c(0.0474, 0.0675, 0.0648, 0.0445, 0.0456, 0.0548, 0.0531, 0.0518, 0.0557, 0.0485, 0.0727, 0.0631, 0.0336, 0.0541, 0.0542, 0.0430, 0.0604, 0.0649)
#Plot
ggplot(df, aes(x = Age, y = Cor, group = Valence, color = Valence)) +
stat_summary(geom="pointrange", position=position_dodge(width = .2), fun.data = NULL) +
stat_summary(geom="line", position=position_dodge(width = .2), fun.data = NULL) +
geom_errorbar(aes(ymin = Cor - ci, ymax = Cor + ci), size= 0.3, width=0.2, position=position_dodge(width = .2)) +
scale_fill_hue(name="Valence Type", breaks=c("Negative", "Mixed", "Positive"), labels=c("Negative", "Mixed", "Positive")) +
scale_x_discrete(name = "",
labels = c("Child", "Adult")) +
scale_y_continuous(limits=c(0.10,0.40)) +
facet_wrap(.~ROI,scales='free',strip.position = "bottom")+
ggtitle("Representational Similarity By Age and Valence") +
labs(y="Mean Correlation Value",x="") +
theme_bw() +
theme(title = element_text(face="bold", size=14)) +
theme(axis.text.x = element_text(size=14, color = "Black")) +
theme(axis.text.y = element_text(size=14, color = "Black")) +
theme(panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.placement = "outside",
strip.text = element_text(size=12,face = 'bold'))
输出:
我最近 运行 一些 2x3 混合方差分析用于一个神经科学项目,年龄和效价作为相关值的预测因子。这些分析 运行 三次,每个 ROI 或大脑区域一次。我可以创建点线图来可视化每个 ROI 的数据,几乎没有问题:
#Dataframe creation and data import
df <- data.frame(matrix(NA, nrow = 6,
ncol = 1,
dimnames = list(1:6, "Age")))
df$Age <-c(rep("Child", 3), rep("Adult", 3))
df$Valence <- c(rep(c("Negative", "Mixed", "Positive"),2))
df$Cor <- c(0.319, 0.303, 0.278, 0.200, 0.230, 0.216)
df$ci <- c(0.0648, 0.0548, 0.0557, 0.0631, 0.0542, 0.0649)
#Plotting
ggplot(df, aes(x = Age, y = Cor, group = Valence, color = Valence)) +
stat_summary(geom="pointrange", position=position_dodge(width = .2), fun.data = NULL) +
stat_summary(geom="line", position=position_dodge(width = .2), fun.data = NULL) +
geom_errorbar(aes(ymin = Cor - ci, ymax = Cor + ci), size= 0.3, width=0.2, position=position_dodge(width = .2)) +
scale_fill_hue(name="Valence Type", breaks=c("Negative", "Mixed", "Positive"), labels=c("Negative", "Mixed", "Positive")) +
scale_x_discrete(name = "Age Groups",
labels = c("Child", "Adult")) +
scale_y_continuous(limits=c(0.10,0.40)) +
ggtitle("Representational Similarity By Age and Valence") +
labs(y="Mean Correlation Value") +
theme_bw() +
theme(title = element_text(face="bold", size=14)) +
theme(axis.text.x = element_text(size=14, color = "Black")) +
theme(axis.text.y = element_text(size=14, color = "Black"))
但是,我想做的是将三个独立的绘图合并为一个,以便所有三个 ROI 的结果都显示在同一个图像中;类似于下图,其中每个 ROI 的结果并排显示: MS Paint Rendering of my Scripting Inadequacies
不过,我没能找到解决此问题的脚本解决方案。我想也许我可以尝试嵌套 X 轴值,因为那是我在概念上所做的,当然那是行不通的。我尝试结合 ROI 和年龄因素,这样我就有了“Child, AMY”、“Adult, AMY”、“Child, NAcc”等,但是使用点线格式,这些线延伸到 ROI,这是不理想。由于对阅读条形图时存在的偏差的研究,点线是我在可视化方面的偏好,但如果有人使用条形图对此有很好的解决方案,我不会反对它。
我在下面包含了一个摘要数据框和我的样板 ggplot 脚本。非常感谢任何帮助,即使它只是告诉我我正在尝试做的事情是不可能的!
#Dataframe creation and data import
df <- data.frame(matrix(NA, nrow = 18,
ncol = 1,
dimnames = list(1:18, "Age")))
df$Age <-c(rep("Child", 9), rep("Adult", 9))
df$Valence <- c(rep(c(rep("Negative",3), rep("Mixed",3), rep("Positive",3)),2))
df$ROI <- rep(c("AMY", "NAcc", "vmPFC"), 6)
df$Cor <- c(0.229, 0.268, 0.319, 0.236, 0.260, 0.303, 0.213, 0.236, 0.278, 0.188, 0.205, 0.200, 0.221, 0.226, 0.230, 0.208, 0.210, 0.216)
df$ci <- c(0.0474, 0.0675, 0.0648, 0.0445, 0.0456, 0.0548, 0.0531, 0.0518, 0.0557, 0.0485, 0.0727, 0.0631, 0.0336, 0.0541, 0.0542, 0.0430, 0.0604, 0.0649)
#Plotting
ggplot(df, aes(x = Age, y = Cor, group = Valence, color = Valence)) +
stat_summary(geom="pointrange", position=position_dodge(width = .2), fun.data = NULL) +
stat_summary(geom="line", position=position_dodge(width = .2), fun.data = NULL) +
geom_errorbar(aes(ymin = Cor - ci, ymax = Cor + ci), size= 0.3, width=0.2, position=position_dodge(width = .2)) +
scale_fill_hue(name="Valence Type", breaks=c("Negative", "Mixed", "Positive"), labels=c("Negative", "Mixed", "Positive")) +
scale_x_discrete(name = "Age Groups",
labels = c("Child", "Adult")) +
scale_y_continuous(limits=c(0.10,0.40)) +
ggtitle("Representational Similarity By Age and Valence") +
labs(y="Mean Correlation Value") +
theme_bw() +
theme(title = element_text(face="bold", size=14)) +
theme(axis.text.x = element_text(size=14, color = "Black")) +
theme(axis.text.y = element_text(size=14, color = "Black"))
你可以试试这个(你必须包装图并调整条带的一些细节)。使用的数据是 df
(嵌套)的最新版本:
library(tidyverse)
#Data
df <- data.frame(matrix(NA, nrow = 18,
ncol = 1,
dimnames = list(1:18, "Age")))
df$Age <-c(rep("Child", 9), rep("Adult", 9))
df$Valence <- c(rep(c(rep("Negative",3), rep("Mixed",3), rep("Positive",3)),2))
df$ROI <- rep(c("AMY", "NAcc", "vmPFC"), 6)
df$Cor <- c(0.229, 0.268, 0.319, 0.236, 0.260, 0.303, 0.213, 0.236, 0.278, 0.188, 0.205, 0.200, 0.221, 0.226, 0.230, 0.208, 0.210, 0.216)
df$ci <- c(0.0474, 0.0675, 0.0648, 0.0445, 0.0456, 0.0548, 0.0531, 0.0518, 0.0557, 0.0485, 0.0727, 0.0631, 0.0336, 0.0541, 0.0542, 0.0430, 0.0604, 0.0649)
#Plot
ggplot(df, aes(x = Age, y = Cor, group = Valence, color = Valence)) +
stat_summary(geom="pointrange", position=position_dodge(width = .2), fun.data = NULL) +
stat_summary(geom="line", position=position_dodge(width = .2), fun.data = NULL) +
geom_errorbar(aes(ymin = Cor - ci, ymax = Cor + ci), size= 0.3, width=0.2, position=position_dodge(width = .2)) +
scale_fill_hue(name="Valence Type", breaks=c("Negative", "Mixed", "Positive"), labels=c("Negative", "Mixed", "Positive")) +
scale_x_discrete(name = "",
labels = c("Child", "Adult")) +
scale_y_continuous(limits=c(0.10,0.40)) +
facet_wrap(.~ROI,scales='free',strip.position = "bottom")+
ggtitle("Representational Similarity By Age and Valence") +
labs(y="Mean Correlation Value",x="") +
theme_bw() +
theme(title = element_text(face="bold", size=14)) +
theme(axis.text.x = element_text(size=14, color = "Black")) +
theme(axis.text.y = element_text(size=14, color = "Black")) +
theme(panel.spacing = unit(0, "points"),
strip.background = element_blank(),
strip.placement = "outside",
strip.text = element_text(size=12,face = 'bold'))
输出: