簡體   English   中英

是否可以為使用 geom_linerange 繪制的不同長度的線(鳥線圖)繪制圖例

[英]Is it possible to draw a legend for the different lengths of lines (birds-on-wire plot) drawn using geom_linerange

我正在使用geom_linerangegeom_linerangeh制作在線鳥類 plot 以顯示受試者隨時間的治療(電線)以及治療期間某些事件(鳥類)的發生。 我用“鳥”的高度(即垂直線的長度)來表示“鳥”(事件)的某種特征。 下面只有 4 種可能的不同長度(參見變量evt_type2 )。 請參閱下面的模擬數據、代碼和 plot。

library(tidyverse)
library(ggstance)
###Mock data###
#Treatment data
data_foo1 <- data.frame(subjectn = c(1, 1, 2, 3, 3, 3, 4, 5),
                        trt_start = c(1, 25, 1, 1, 50, 101, 1, 1),
                        trt_end = c(80, 60, 100, 25, 100, 200, 120, 90),
                        trt_type = as.factor(c(1, 2, 1, 3, 4, 5, 2, 4)),
                        stringsAsFactors =  F)

#Some kind of events data
data_foo2 <- data.frame(subjectn = c(1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5),
                        evt_start = c(70, 20, 90, 92, 24, 50, 70, 120, 170, 69, 80, 90),
                        evt_type1 = as.factor(c(0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0)),
                        evt_type2 = c(0.2, 0.8, 0.2, 0.4, 0.4, 0.2, 0.2, 0.6, 0.4, 0.4, 0.6, 0.2))

###Plot###
data_foo2 %>% 
  ggplot() + 
  geom_linerange(aes(x = evt_start, y = subjectn, ymin = subjectn, ymax = subjectn + evt_type2, linetype = evt_type1), size=0.8,  alpha = 0.5) +
  geom_linerangeh(data = data_foo1, aes(x = trt_start, y = subjectn, xmin = trt_start, xmax = trt_end, color = trt_type), size = 1.2, alpha = 0.7) +
  scale_y_continuous(breaks = c(1, 2, 3, 4, 5), labels = c("A001", "A002", "A003", "A004", "A005")) +
  xlab("Time") + theme_bw()

在此處查看生成的 plot

在此處查看結果圖

我的問題是,是否可以為 4 種不同的垂直線長度添加圖例? 謝謝你!

正如評論中提到的,在圖例中顯示線條的長度通常是不容易的。 一種選擇是創建一個假圖例 - 另一個 plot,然后將其添加到主 plot。

但我很難找到非常引人注目的可視化。 線條長度很難辨別,特別是當您有不同的線條類型並且線條可能會奇怪地切斷時(參見主題 A003)。 在圖例中也不清楚如何將 map 各自的線長到 plot 中的長度。

因此,我建議使用不同的美學來可視化維度事件 2。一種方法是繪制矩形而不是線條並使用填充。 您可以將此分類(如在我的示例中)或連續,並且圖例很容易映射到您的數據 - 在我看來,您可以更好地識別四種不同的事件類型。

library(tidyverse)
library(ggstance)

ggplot() +
  geom_linerangeh(
    data = data_foo1,
    aes(
      y = subjectn,
      xmin = trt_start,
      xmax = trt_end,
      color = trt_type
    ),
    size = 1.2, alpha = 0.7
  ) +
  geom_rect(
    data = data_foo2, aes(
      xmin = evt_start - 2,
      xmax = evt_start + 2,
      ymin = subjectn,
      ymax = subjectn + 0.5,
      linetype = evt_type1,
      fill = as.character(evt_type2)
    ),
    size = 0.2, color = "black", alpha = 0.5
  ) +
  scale_fill_brewer() +
  scale_y_continuous(breaks = 1:5, labels = paste0("A00", 1:5)) +
  theme_bw()

或者,您可以保留線條,並使用ggnewscale添加第二種顏色美感。

ggplot() +
  geom_linerangeh(
    data = data_foo1,
    aes(
      y = subjectn,
      xmin = trt_start,
      xmax = trt_end,
      color = trt_type
    ),
    size = 1.2, alpha = 0.7
  ) +
  scale_y_continuous(breaks = 1:5, labels = paste0("A00", 1:5)) +
  ggnewscale::new_scale_color()+
  geom_linerange(data = data_foo2, 
                 aes(x = evt_start, 
                     y = subjectn, 
                     ymin = subjectn, 
                     ymax = subjectn + 0.5, 
                     color = as.character(evt_type2),
                     linetype = evt_type1),
                 size=0.8) +
  scale_color_brewer() +
  theme_bw()

代表 package (v0.3.0) 於 2020 年 4 月 26 日創建

colors 非常“亮”,如果你想讓它們“更暗”,你可以使用shades包,例如,通過將 scale_color function 包裝到亮度修改函數之一中,例如shades::brightness(scale_color_brewer(), shades::delta(-0.2))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM