如何将不同的 stat_smooths 分配给不同的方面

How to assign different stat_smooths to different facets

我又遇到了一个不同的问题。我想将不同的 stat_smooth 分配给不同的方面。在这种情况下,我想保持 Walking 和 Transit 结构不变,但使 Automobile 二次方。

以下是我的数据:

 df <- structure(list(commutedistance = c(
  11.3, 8, 7.9, 6, 7.1, 4.8,
  9.3, 6.1, 8.7, 7.2, 10.1, 8.1, 9.5, 7, 11.3, 9.3, 10.4, 8.2,
  11.2, 8.9, 13.3, 10.4, 14.7, 11.3, 11.3, 8.9, 9.7, 6.5, 9.7,
  6.5, 12.8, 8.8, 13.2, 9.6, 12.5, 8.4, 11.4, 8.4, 10.5, 7.3, 10.8,
  7.5, 11, 7.5, 11, 7.5, 11.5, 6.8, 11.8, 8.8, 12.7, 7.7, 12, 11.8,
  9.9, 12.2, 10.5, 11.4, 10.9, 9.6, 12.7, 12.2, 11.7, 11.8, 12.1,
  11.9, 13.8, 12.9, 12.6, 14.2, 6.9, 4.6, 13, 6.4, 13.2, 9.3, 12.7,
  10.6, 14.1, 9.6, 14.4, 8.2, 13, 8.7, 12, 8.8, 13, 7, 10.1, 5.9,
  12.1, 15.3, 12.2, 9.8, 15.6, 10.9, 14.4, 9.5, 13.1, 8.4, 15.2,
  10.4, 12.3, 7.1, 10.5, 8.5, 16.1, 9.4, 13.4, 11.3, 13.6, 9.3,
  17.5, 13.1, 14.9, 9.1, 8.7, 5.8, 8.9, 8.7, 13.4, 12.6, 13.7,
  10.8, 10.9, 7.8, 13.6, 8.7, 7.5, 7.6, 11.5, 4.9, 5.6, 4.3, 7.7,
  7.5
), carcommute = c(
  35.9, 84, 28.3, 82.6, 23.3, 63, 34.2, 80.7,
  31.1, 76.1, 29.9, 78.4, 32.2, 79.8, 31.7, 82.9, 34.1, 79.7, 28.5,
  76.3, 32.2, 81.9, 36.6, 85.5, 34.4, 83.3, 36.7, 91, 33.8, 82.5,
  47, 89.9, 41.1, 87.8, 33.6, 90.4, 38.9, 86.7, 32.1, 84.4, 32.2,
  88.7, 32.9, 87.5, 41.2, 89.1, 42.6, 87.6, 42.2, 84.2, 40.2, 88.5,
  59.9, 76.9, 59.4, 75.8, 57, 67.1, 56.9, 76.2, 67.9, 78.7, 60.7,
  75.2, 50.6, 81.5, 61, 76.5, 67.2, 83.5, 60.7, 49.7, 70.4, 87.4,
  61.6, 80.1, 62.7, 79.6, 50.4, 67, 50.1, 83.1, 51, 75.1, 52.1,
  78.3, 46, 77.7, 74, 80.7, 62.4, 88.1, 66.4, 81.6, 59.8, 81.5,
  52.4, 83.6, 55, 80.5, 61.5, 82.3, 73.5, 89.5, 74, 81.8, 50.6,
  79, 60.5, 82.8, 55.3, 77.8, 68.7, 82.1, 69.9, 86.6, 78.9, 73.9,
  76.8, 76.3, 72, 86.4, 74.1, 86.3, 74.6, 78.6, 65, 90.5, 73.2,
  79, 73.6, 63, 74.7, 72.7, 77.4, 79.2
), Fabric = c(
  "Walking",
  "Walking", "Transit", "Transit", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Walking", "Walking", "Walking",
  "Walking", "Transit", "Transit", "Walking", "Walking", "Walking",
  "Walking", "Walking", "Walking", "Automobile", "Automobile",
  "Automobile", "Automobile", "Walking", "Transit", "Walking",
  "Walking", "Transit", "Transit", "Transit", "Transit", "Walking",
  "Walking", "Transit", "Transit", "Transit", "Transit", "Automobile",
  "Automobile", "Automobile", "Automobile", "Automobile", "Automobile",
  "Transit", "Transit", "Transit", "Transit", "Transit", "Transit",
  "Walking", "Walking", "Walking", "Walking", "Transit", "Transit",
  "Automobile", "Automobile", "Automobile", "Automobile", "Transit",
  "Transit", "Transit", "Transit", "Automobile", "Automobile",
  "Transit", "Transit", "Automobile", "Automobile", "Transit",
  "Transit", "Automobile", "Automobile", "Transit", "Transit",
  "Automobile", "Automobile", "Automobile", "Automobile", "Automobile",
  "Automobile", "Automobile", "Automobile", "Automobile", "Automobile",
  "Automobile", "Automobile", "Automobile", "Automobile", "Automobile",
  "Automobile", "Automobile", "Automobile", "Automobile", "Automobile",
  "Automobile", "Automobile", "Automobile", "Automobile", "Automobile",
  "Automobile", "Automobile", "Automobile"
)), row.names = c(
  NA,
  -136L
), class = "data.frame")

我的代码也是这样的:

library(tidyverse)
library(ggpubr)
df %>%
  ggplot(aes(commutedistance, carcommute, color = Fabric, shape = Fabric)) +
  geom_point(size = 3.5)+
  stat_smooth(method = "lm", se = FALSE) +
  facet_wrap(~Fabric, ncol=2, scales = "free")+
  theme(strip.text.x = element_text(size = 12, colour = "black", angle = 0, face = "bold"))+
  theme(axis.title.x = element_text(color="black", size=12, face="bold"),
        axis.title.y = element_text(color="black", size=12, face="bold"))+
  theme(axis.text.x = element_text(colour = "black", size = 12), 
        axis.text.y = element_text(colour = "black", size = 12))+
  scale_y_continuous(labels = function(x) paste0(x, "%"))+
  labs(x = "Commute Distance (km)",
       y = "Share of commuters getting to work by car")+
  theme(legend.position = c(1, 0),
        legend.justification = c(1, 0))+
  stat_cor(
    aes(label = paste(..rr.label.., ..p.label.., sep = "~`,`~")), 
    label.x = 5
  )+
  stat_regline_equation(label.x = 10, label.y = 90)+
  scale_color_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))+
  scale_fill_manual(values = c("#00AFBB", "#E7B800", "#FC4E07"))

在 运行 上面的代码之后,我得到了下图。如何将公交和步行保持为线性回归模型并将汽车回归转换为二次模型。我将非常感谢您的意见!!!

要获得不同的构面模型,您可以使用不同的 stat_smooth 图层,如下所示:

library(ggplot2)
library(dplyr)

df <- mutate(df, model = ifelse(Fabric == "Automobile", "quadratic", "lm"))

df %>% 
  ggplot(aes(commutedistance, carcommute, color = Fabric, shape = Fabric)) +
  geom_point(size = 3.5)+
  stat_smooth(data = filter(df, model == "lm"), method = "lm", se = FALSE) +
  stat_smooth(data = filter(df, model == "quadratic"), method = "lm", formula = y ~ poly(x, 2), se = FALSE) +
  facet_wrap(~Fabric, ncol=2, scales = "free")
#> `geom_smooth()` using formula 'y ~ x'

reprex package (v0.3.0)

于 2020-06-16 创建