R如何翻转facet_grid行?
R how to flip facet_grid rows?
我有下面的情节,希望大人排在底部,年轻人排在上面。
数据:
structure(list( Gender = structure(c(1L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 1L), .Label = c("Male", "Female"), class = "factor"),
Age = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("Younger",
"Older"), class = "factor"), SleepDeprivation = structure(c(2L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
ROI = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Right Noradrenergic Bundle", class = "factor"),
FA = c(0.270336896, 0.239859328, 0.253058553, 0.239136323,
0.236547187, 0.250703156, 0.240607098, 0.256228983, 0.249909833,
0.25690344, 0.263866663, 0.240790352, 0.25867039, 0.295251846,
0.215813458, 0.252566397, 0.281900764, 0.23559688, 0.242475167,
0.282497048, 0.259372681, 0.294507146, 0.288260996, 0.239144236,
0.223323226, 0.253974199, 0.240887016, 0.253256589, 0.223256662,
0.235150456, 0.291969478, 0.305382729, 0.270458698, 0.232845142,
0.28080976, 0.264537901, 0.258622199, 0.271028847, 0.225935951,
0.250249714, 0.283011079, 0.278435558, 0.279990733, 0.233835503,
0.238910407, 0.300289392, 0.274696231, 0.288767546, 0.223124981,
0.256094754, 0.239295736, 0.270433217, 0.299104929)), row.names = c(NA,
-53L), class = "data.frame")
我的代码:
library(tidyverse)
library(wesanderson)
library(ggplot2)
DF$Age <- factor(DF$Age, levels = c("Younger", "Older"))
我注意到我的 geom_text 导致了这个问题。由于我最初省略了该代码并且下面的答案能够产生正确的情节。
geomtext 标签:
labelp4w <- data.frame(
label = c(c(paste0('atop(~italic(t)[Student](15.95) == -0.93)'),
paste0("italic(p) == 0.36")),
c(paste0('atop(italic(t)[Student](30.15) == 0.51)'),
paste0("italic(p) == 0.61"))),
Age = c(c("Younger", "Younger"),
c("Older","Older")))
## plot
(p4 <- ggplot(data = DF, aes(x=SleepDeprivation, y=FA)) +
theme_minimal() +
facet_grid(cols = vars(ROI), rows = vars(Age),
labeller = as_labeller(c("Younger" = "Younger Adults",
"Older" = "Older Adults",
"title" ="title")))+
geom_violin(lwd = 1, alpha = 0.5, trim=F, aes(fill = Age)) +
geom_boxplot(width = 0.1) +
scale_fill_manual(values=wes_palette(n=2, name="GrandBudapest2")) +
geom_text(data = labelp4w,
x = 1.5,
y=seq(0.75,0.68, length = 4),
label = labelp4w$label,
size = 7.5, parse = T) +
scale_y_continuous(breaks = seq(0.2, 0.8, 0.2),limits = (c(0.2, 0.8))) +
theme(plot.title = element_text(hjust = 0.3, size=40, face="bold", margin=margin(0,0,0,0)),
legend.position = "none",
legend.text = element_blank(),
legend.title = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 25, face = "bold"),
axis.text.x = element_text(size = 25, face = "bold"),
axis.text.y = element_blank(),
strip.text.x = element_text(size = 28, face = "bold"),
strip.text.y = element_text(size = 20, face="bold"),
plot.subtitle = element_text(hjust = 0.5, size = 20),
plot.caption = element_text(color = "black", size =20),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_blank()) +
labs(x = "")
)
添加 geom_text 时出现此问题。我不确定为什么?
这似乎是你这边造成的。
DF$Age <- factor(DF$Age, levels = c("Younger", "Older"))
ggplot(data = DF, aes(x=SleepDeprivation, y=FA)) +
facet_grid(cols = vars(ROI), rows = vars(Age),
labeller = as_labeller(c("Younger" = "Younger Adults",
"Older" = "Older Adults",
"title" ="title")))+
geom_violin(lwd = 1, alpha = 0.5, trim=F, aes(fill = Age)) +
geom_boxplot(width = 0.1) +
labs(x = "")
上面我去掉了所有乱七八糟的代码,不影响你的定位。
更新 geom_text
有了 geom_text 包含,这似乎是对 2 个数据集执行排序时之间的模糊交互。请注意,DF$Age
是一个因子,而 labelp4w$Age
是一个字符向量。在这种情况下,排序来自后者。只要确保两者都是因数即可。
labelp4w <- data.frame(
label = c(c(paste0('atop(~italic(t)[Student](15.95) == -0.93)'),
paste0("italic(p) == 0.36")),
c(paste0('atop(italic(t)[Student](30.15) == 0.51)'),
paste0("italic(p) == 0.61"))),
Age = factor(c("Younger", "Younger", "Older", "Older"), levels = c("Younger", "Older")))
我有下面的情节,希望大人排在底部,年轻人排在上面。
数据:
structure(list( Gender = structure(c(1L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 1L), .Label = c("Male", "Female"), class = "factor"),
Age = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L), .Label = c("Younger",
"Older"), class = "factor"), SleepDeprivation = structure(c(2L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
ROI = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Right Noradrenergic Bundle", class = "factor"),
FA = c(0.270336896, 0.239859328, 0.253058553, 0.239136323,
0.236547187, 0.250703156, 0.240607098, 0.256228983, 0.249909833,
0.25690344, 0.263866663, 0.240790352, 0.25867039, 0.295251846,
0.215813458, 0.252566397, 0.281900764, 0.23559688, 0.242475167,
0.282497048, 0.259372681, 0.294507146, 0.288260996, 0.239144236,
0.223323226, 0.253974199, 0.240887016, 0.253256589, 0.223256662,
0.235150456, 0.291969478, 0.305382729, 0.270458698, 0.232845142,
0.28080976, 0.264537901, 0.258622199, 0.271028847, 0.225935951,
0.250249714, 0.283011079, 0.278435558, 0.279990733, 0.233835503,
0.238910407, 0.300289392, 0.274696231, 0.288767546, 0.223124981,
0.256094754, 0.239295736, 0.270433217, 0.299104929)), row.names = c(NA,
-53L), class = "data.frame")
我的代码:
library(tidyverse)
library(wesanderson)
library(ggplot2)
DF$Age <- factor(DF$Age, levels = c("Younger", "Older"))
我注意到我的 geom_text 导致了这个问题。由于我最初省略了该代码并且下面的答案能够产生正确的情节。
geomtext 标签:
labelp4w <- data.frame(
label = c(c(paste0('atop(~italic(t)[Student](15.95) == -0.93)'),
paste0("italic(p) == 0.36")),
c(paste0('atop(italic(t)[Student](30.15) == 0.51)'),
paste0("italic(p) == 0.61"))),
Age = c(c("Younger", "Younger"),
c("Older","Older")))
## plot
(p4 <- ggplot(data = DF, aes(x=SleepDeprivation, y=FA)) +
theme_minimal() +
facet_grid(cols = vars(ROI), rows = vars(Age),
labeller = as_labeller(c("Younger" = "Younger Adults",
"Older" = "Older Adults",
"title" ="title")))+
geom_violin(lwd = 1, alpha = 0.5, trim=F, aes(fill = Age)) +
geom_boxplot(width = 0.1) +
scale_fill_manual(values=wes_palette(n=2, name="GrandBudapest2")) +
geom_text(data = labelp4w,
x = 1.5,
y=seq(0.75,0.68, length = 4),
label = labelp4w$label,
size = 7.5, parse = T) +
scale_y_continuous(breaks = seq(0.2, 0.8, 0.2),limits = (c(0.2, 0.8))) +
theme(plot.title = element_text(hjust = 0.3, size=40, face="bold", margin=margin(0,0,0,0)),
legend.position = "none",
legend.text = element_blank(),
legend.title = element_blank(),
axis.title.y = element_blank(),
axis.title.x = element_text(size = 25, face = "bold"),
axis.text.x = element_text(size = 25, face = "bold"),
axis.text.y = element_blank(),
strip.text.x = element_text(size = 28, face = "bold"),
strip.text.y = element_text(size = 20, face="bold"),
plot.subtitle = element_text(hjust = 0.5, size = 20),
plot.caption = element_text(color = "black", size =20),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_blank()) +
labs(x = "")
)
添加 geom_text 时出现此问题。我不确定为什么?
这似乎是你这边造成的。
DF$Age <- factor(DF$Age, levels = c("Younger", "Older"))
ggplot(data = DF, aes(x=SleepDeprivation, y=FA)) +
facet_grid(cols = vars(ROI), rows = vars(Age),
labeller = as_labeller(c("Younger" = "Younger Adults",
"Older" = "Older Adults",
"title" ="title")))+
geom_violin(lwd = 1, alpha = 0.5, trim=F, aes(fill = Age)) +
geom_boxplot(width = 0.1) +
labs(x = "")
上面我去掉了所有乱七八糟的代码,不影响你的定位。
更新 geom_text
有了 geom_text 包含,这似乎是对 2 个数据集执行排序时之间的模糊交互。请注意,DF$Age
是一个因子,而 labelp4w$Age
是一个字符向量。在这种情况下,排序来自后者。只要确保两者都是因数即可。
labelp4w <- data.frame(
label = c(c(paste0('atop(~italic(t)[Student](15.95) == -0.93)'),
paste0("italic(p) == 0.36")),
c(paste0('atop(italic(t)[Student](30.15) == 0.51)'),
paste0("italic(p) == 0.61"))),
Age = factor(c("Younger", "Younger", "Older", "Older"), levels = c("Younger", "Older")))