R 如何使用图例在ggplot2中创建分段图?

R 如何使用图例在ggplot2中创建分段图?,r,ggplot2,regression,R,Ggplot2,Regression,我的数据如下: 我想创建一个分段的情节(如预绘图和后绘图,包括t=10处的垂直线,以指示变化。t表示经过的时间,x表示0表示预实施,1表示后实施和count\u visit\u triage\\d是我希望在y轴上绘制的计数数据 这是我的r代码。我将多个geom_smooth拼成了同一个图形,每个颜色代表triage1、triage2等的值。因此,我无法获得图例。我的问题是(1)如何简化此代码,以便图例可以包含在图形中 ggplot(df, aes(x = t, y = count_visit_

我的数据如下:

我想创建一个分段的情节(如预绘图和后绘图,包括t=10处的垂直线,以指示变化。
t
表示经过的时间,
x
表示0表示预实施,1表示后实施和
count\u visit\u triage\\d
是我希望在y轴上绘制的计数数据

这是我的r代码。我将多个
geom_smooth
拼成了同一个图形,每个颜色代表
triage1
triage2
等的值。因此,我无法获得图例。我的问题是(1)如何简化此代码,以便图例可以包含在图形中

ggplot(df, aes(x = t, y = count_visit_triage1)) +
  geom_smooth(data = subset(df, x == 0), aes(x = t, y = count_visit_triage1), colour = "blue", se = F) +
  geom_smooth(data = subset(df, x == 1), aes(x = t, y = count_visit_triage1), colour = "blue", se = F) +
  geom_smooth(data = subset(df, x == 0), aes(x = t, y = count_visit_triage2), colour = "orange", se = F) +
  geom_smooth(data = subset(df, x == 1), aes(x = t, y = count_visit_triage2), colour = "orange", se = F) +
  geom_smooth(data = subset(df, x == 0), aes(x = t, y = count_visit_triage3), colour = "green", se = F) +
  geom_smooth(data = subset(df, x == 1), aes(x = t, y = count_visit_triage3), colour = "green", se = F) +
  geom_smooth(data = subset(df, x == 0), aes(x = t, y = count_visit_triage4), colour = "red", se = F) +
  geom_smooth(data = subset(df, x == 1), aes(x = t, y = count_visit_triage4), colour = "red", se = F) +
  geom_vline(xintercept = 10, linetype = "dashed") + 
  theme_bw()

数据

df <- structure(list(t = 1:20, x = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), count_visit_triage1 = c(42L, 
55L, 61L, 52L, 58L, 38L, 47L, 46L, 66L, 44L, 24L, 17L, 40L, 25L, 
18L, 23L, 34L, 35L, 22L, 23L), count_visit_triage2 = c(175L, 
241L, 196L, 213L, 189L, 163L, 181L, 166L, 229L, 224L, 153L, 139L, 
125L, 145L, 134L, 115L, 152L, 153L, 136L, 154L), count_visit_triage3 = c(120L, 
114L, 106L, 88L, 108L, 103L, 103L, 93L, 80L, 81L, 88L, 94L, 94L, 
77L, 91L, 100L, 93L, 70L, 79L, 77L), count_visit_triage4 = c(3L, 
0L, 0L, 1L, 2L, 2L, 0L, 4L, 4L, 2L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
0L, 1L, 2L)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", 
"data.frame"))

df重塑数据,然后指定
col

library(tidyverse)

df %>%
  pivot_longer(starts_with("count_")) %>%
  ggplot(aes(t, value, col = name, group = paste(x, name))) +
    geom_smooth(se = FALSE) +
    geom_vline(xintercept = 10, linetype = "dashed") + 
    theme_bw()
您可以尝试以下方法:

library(tidyverse)
df %>% 
  pivot_longer(cols = -c(t,x),
               names_to = "visit",
               values_to = "count") %>%
  ggplot() +
  geom_line(aes(x = t, 
                y = count, 
                color = visit,
                group = interaction(x,visit))) +
  geom_vline(xintercept = 10, linetype = "dashed") + 
  scale_color_manual(name = "legend",
                     values = 1:4,
                     labels = c("Visit Triage 1",
                                "Visit Triage 2",
                                "Visit Triage 3",
                                "Visit Triage 4")) +
  theme_bw()


谢谢Paul,什么是
美学?aes中的
(美学映射)确定哪些点
geom_smooth
应组合成一条直线。在此代码中,具有相同
x
名称的点将组合成一条直线。
交互
位于
基础
中?是。
交互
是将两个字符串组合成一个因子的正确方法,而不是像我一样粘贴