繁体   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