简体   繁体   English

是否可以为使用 geom_linerange 绘制的不同长度的线(鸟线图)绘制图例

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

I am making a birds-on-wire plot using geom_linerange and geom_linerangeh to show subjects' treatment over time (wire) and the occurrence of some events (birds) during the treatment period.我正在使用geom_linerangegeom_linerangeh制作在线鸟类 plot 以显示受试者随时间的治疗(电线)以及治疗期间某些事件(鸟类)的发生。 I am using the height of "birds" (ie length of vertical lines) to represent a certain characteristic of the "birds" (events).我用“鸟”的高度(即垂直线的长度)来表示“鸟”(事件)的某种特征。 There are only 4 possible different lengths (see variable evt_type2 ) below.下面只有 4 种可能的不同长度(参见变量evt_type2 )。 See mock data, code, and plot below.请参阅下面的模拟数据、代码和 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()

See the resulting plot here在此处查看生成的 plot

在此处查看结果图

My question is, is it possible to add a legend for the 4 different vertical line lengths?我的问题是,是否可以为 4 种不同的垂直线长度添加图例? THANK YOU!谢谢你!

As mentioned in the comment, showing lengths of lines in the legend is generally not easily possible.正如评论中提到的,在图例中显示线条的长度通常是不容易的。 One option would be to create a fake legend - another plot, which you then add to your main plot.一种选择是创建一个假图例 - 另一个 plot,然后将其添加到主 plot。

But I struggle to find the visualisation very compelling.但我很难找到非常引人注目的可视化。 Line lengths are difficult to discern, expecially when you have different line types and a line can cut off weirdly (see subject A003).线条长度很难辨别,特别是当您有不同的线条类型并且线条可能会奇怪地切断时(参见主题 A003)。 It would also not be clear in the legend how to map the respective line length to the legnth in the plot.在图例中也不清楚如何将 map 各自的线长到 plot 中的长度。

Thus, I recommend using a different aesthetic for visualising dimension event 2. One way would be to draw rectangles instead of lines and use the fill.因此,我建议使用不同的美学来可视化维度事件 2。一种方法是绘制矩形而不是线条并使用填充。 You can make this categorical (as in my example) or continuous, and the legend easily maps to your data - visually, in my opinion, you can better discern the four different event types.您可以将此分类(如在我的示例中)或连续,并且图例很容易映射到您的数据 - 在我看来,您可以更好地识别四种不同的事件类型。

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

Or, you can keep the lines, and add a second color aesthetic with ggnewscale .或者,您可以保留线条,并使用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()

Created on 2020-04-26 by the reprex package (v0.3.0)代表 package (v0.3.0) 于 2020 年 4 月 26 日创建

The colors come out quite "light", and if you want them "darker", you can use the shades packages, eg, by wrapping your scale_color function into one of the brightness modifying functions, eg shades::brightness(scale_color_brewer(), shades::delta(-0.2)) 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