如何在不同方面为 ggplot stat_smooth 设置不同的分组美学

How to set different grouping aesthetics for ggplot stat_smooth in different facet

我有以下数据:

structure(list(patient_code = c("BD1200", "BD1200", "BD1200", 
"BD1200", "BD1402", "BD1402", "BD1402", "BD1402", "CA1601", "CA1601", 
"CA1601", "CA1601", "CS1701", "CS1701", "CS1701", "CS1701", "CE1801", 
"CE1801", "CE1801", "CE1801", "DE1511", "DE1511", "DE1511", "DE1511", 
"LM1400", "LM1400", "LM1400", "LM1400", "MJ1402", "MJ1402", "MJ1402", 
"MJ1402", "PP1511", "PP1511", "PP1511", "PP1511", "RA1401", "RA1401", 
"RA1401", "RA1401", "RC1400", "RC1400", "RC1400", "RC1400", "SO1701", 
"SO1701", "SO1701", "SO1701", "SS1801", "SS1801", "SS1801", "SS1801"
), age = c(91, 91, 91, 91, 73, 73, 73, 73, 56, 56, 56, 56, 46, 
46, 46, 46, 38, 38, 38, 38, 61, 61, 61, 61, 71, 71, 71, 71, 76, 
76, 76, 76, 67, 67, 67, 67, 74, 74, 74, 74, 71, 71, 71, 71, 41, 
41, 41, 41, 40, 40, 40, 40), sex = c("M", "M", "M", "M", "F", 
"F", "F", "F", "M", "M", "M", "M", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "M", "M", "M", 
"M", "M", "M", "M", "M", "M", "M", "M", "M", "F", "F", "F", "F", 
"M", "M", "M", "M", "F", "F", "F", "F"), height = c(170, 170, 
170, 170, 156, 156, 156, 156, 180, 180, 180, 180, 162, 162, 162, 
162, 169, 169, 169, 169, 165, 165, 165, 165, 153, 153, 153, 153, 
185, 185, 185, 185, 168, 168, 168, 168, 173, 173, 173, 173, 158, 
158, 158, 158, 182, 182, 182, 182, 165, 165, 165, 165), weight = c(72, 
72, 72, 72, 53, 53, 53, 53, 106, 106, 106, 106, 69, 69, 69, 69, 
66, 66, 66, 66, 52, 52, 52, 52, 45, 45, 45, 45, 110, 110, 110, 
110, 92, 92, 92, 92, 85, 85, 85, 85, 74, 74, 74, 74, 96.5, 96.5, 
96.5, 96.5, 67, 67, 67, 67), bmi = c(24.9134948096886, 24.9134948096886, 
24.9134948096886, 24.9134948096886, 21.7784352399737, 21.7784352399737, 
21.7784352399737, 21.7784352399737, 32.716049382716, 32.716049382716, 
32.716049382716, 32.716049382716, 26.291723822588, 26.291723822588, 
26.291723822588, 26.291723822588, 23.1084345786212, 23.1084345786212, 
23.1084345786212, 23.1084345786212, 19.1000918273646, 19.1000918273646, 
19.1000918273646, 19.1000918273646, 19.2233756247597, 19.2233756247597, 
19.2233756247597, 19.2233756247597, 32.1402483564646, 32.1402483564646, 
32.1402483564646, 32.1402483564646, 32.5963718820862, 32.5963718820862, 
32.5963718820862, 32.5963718820862, 28.4005479635136, 28.4005479635136, 
28.4005479635136, 28.4005479635136, 29.6426854670726, 29.6426854670726, 
29.6426854670726, 29.6426854670726, 29.1329549571308, 29.1329549571308, 
29.1329549571308, 29.1329549571308, 24.6097337006428, 24.6097337006428, 
24.6097337006428, 24.6097337006428), variable = c("muscle_fat_mm3", 
"muscle_ff_from_segmentation", "muscle_mm3", "subcut_fat_mm3", 
"muscle_fat_mm3", "muscle_ff_from_segmentation", "muscle_mm3", 
"subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3", "muscle_fat_mm3", "muscle_ff_from_segmentation", 
"muscle_mm3", "subcut_fat_mm3"), value = c(478569.3359375, 45.600405239292, 
4169956.0546875, 1476489.2578125, 376596.6796875, 31.5671469350962, 
2521455.078125, 2642592.7734375, 604804.6875, 59.1302753496503, 
6097094.7265625, 3302670.8984375, 572958.984375, 11.596700038243, 
3446499.0234375, 3746103.515625, 185146.484375, 35.0908397071678, 
3897255.859375, 3434550.78125, 310375.9765625, 31.8352193509615, 
3090175.78125, 1303105.46875, 407695.3125, 23.0094809877622, 
2032436.5234375, 1430712.890625, 1012875.9765625, 46.6928064903846, 
4884965.8203125, 2261372.0703125, 796171.875, 54.2822574300699, 
4961044.921875, 2377094.7265625, 610234.375, 25.5995147235577, 
3695327.1484375, 1061621.09375, 421166.9921875, 35.237383631993, 
3163217.7734375, 3767353.515625, 453144.53125, 16.9699877076049, 
6104433.59375, 3296123.046875, 404785.15625, 9.00698576813811, 
3238256.8359375, 3941044.921875)), row.names = c(NA, -52L), class = c("tbl_df", 
"tbl", "data.frame"))

如果我使用构面为 variableBMI 中每个变量的关联创建散点图,显示按 sex 分组的拟合线如下:

library(ggplot2)
ggplot(demo_index, aes(x = bmi, y = value, color = sex)) +
  geom_point(size = 1.5) +
  stat_smooth(aes(group = sex), method = "lm", se = FALSE, size = 1.5) + 
  facet_wrap(~variable, scales = "free") +
  ggtitle("Age associations") + 
  theme_bw() + 
  theme(panel.grid.minor = element_blank(),
        text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5),
        axis.title.y = element_blank())

我得到了以下情节

很明显,虽然某些协会更适合区分男性和女性,但某些其他协会在不分组的情况下更适合。 我的问题是:在 ggplot 中有没有一种方法可以像上面那样做多面散点图,其中 stat_smooth 的分组变量在多面之间变化?

您不需要更改每个构面的分组变量,您只需要使用在该构面中包含您想要的组的分组变量。

library(tidyverse)

demo_index %>% 
  mutate(grp = if_else(str_starts(variable, "muscle_ff"), "both", sex)) %>% 
  ggplot(aes(x = bmi, y = value)) +
  geom_point(aes(color = sex), size = 1.5) +
  stat_smooth(aes(color = grp, group = grp), method = "lm", se = FALSE, size = 1.5) + 
  facet_wrap(~variable, scales = "free") +
  ggtitle("Age associations") + 
  theme_bw() + 
  theme(panel.grid.minor = element_blank(),
        text = element_text(size = 10),
        plot.title = element_text(hjust = 0.5),
        axis.title.y = element_blank())

我确实稍微调整了 aes(color) 映射,以便正确显示图例。

注意:对于您所在领域之外的人来说,哪些组应该一起建模并不那么明显,所以我做了我最好的猜测..