簡體   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