繁体   English   中英

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

[英]How to create segmented graphs in ggplot2 with legend?

我有一个数据如下: 在此处输入图片说明

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

这是我的 r 代码。 我将多个geom_smooth拼凑成同一个图形,每种颜色代表来自triage1triage2等的值。因此,我无法获得图例。 我的问题是(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"))

重塑数据,然后指定colgroup美学。

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()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM