繁体   English   中英

在图例中添加文本,而无需参考图中的线

[英]Add text to plot legend without referring to line in plot

我有一条包含两行的图,其中的图例条目非常相似,除了一些单词(请参见下文)。

我的代码如下所示:

library(tidyverse)

set.seed(36)
Data <- data.frame(
  date = sample((as.Date(as.Date("2011-12-30"):as.Date("2012-01-04"),
                         origin="1970-01-01")), 1000, replace = TRUE),
  group = sample(c("0", "1"), 1000, replace = TRUE),
  outcome = sample(c("0", "1"), 1000, replace = TRUE))
Data %>%
  mutate(date = as.POSIXct(date), group = factor(group)) %>%
  group_by(group, date) %>% #group
  summarise(prop = sum(outcome == "1")/n()) %>% #calculate proportion 
  ggplot()+
  geom_line(aes(x = date, y = prop, color = group))+
  geom_point(aes(x = date, y = prop, color = group)) +
  scale_color_manual(values = c("0" = "blue", "1" = "black"), labels = c("0" = 'Some text that repeats, the only unique thing being "A"', "1" = 'Some text that repeats, the only unique thing being "B"'), name = "Legend")

它产生如下图: 样地

我想简化传说。 它应包含以下几行:

  1. 标题( Legend ),
  2. 没有任何彩色线条的Some text that repeats, the only unique thing beingSome text that repeats, the only unique thing being ),然后输入带有
  3. 带有文字A和的蓝线
  4. 黑色文字B

即所需的输出看起来像这样: 所需的输出

我该如何实现?

这是一个建议:

scale_color_manual(values = c("0" = "blue", "1" = "black"), 
                   labels = c("0" = '"A"', "1" = '"B"'), 
                   name = "Legend\nSome text that repeats,\nthe only unique thing being")

但我会删除"Legend" (不是非常有用的恕我直言)。

在此处输入图片说明

为了清楚地区分"Legend"和字幕文本,可以使用expressionatop其加粗:

legend_title = expression(atop(bold("Legend\n"),
                         "Some text that repeats,\nthe only unique thing being"))

Data %>%
  mutate(date = as.POSIXct(date), group = factor(group)) %>%
  group_by(group, date) %>% #group
  summarise(prop = sum(outcome == "1")/n()) %>% #calculate proportion 
  ggplot()+
  geom_line(aes(x = date, y = prop, color = group))+
  geom_point(aes(x = date, y = prop, color = group)) +
  scale_color_manual(values = c("0" = "blue", "1" = "black"), 
                     labels = c("0" = '"A"', "1" = '"B"'), 
                     name = legend_title)

在此处输入图片说明

然后,您可以在"Legend"之后添加空格以将其向左对齐:

legend_title = expression(atop(bold("Legend                             \n"),
                         "Some text that repeats,\nthe only unique thing being"))

在此处输入图片说明

这只是一种小技巧:使用fct_expandforcats (包含在tidyverse中)将虚拟级别添加到您的group变量中,然后使用fct_relevel使其成为因子中的第一级别。 这样,您将获得一个虚假的观察结果,该观察结果将列在图例的顶部,但实际上并没有附带任何数据。

除了添加该行外,我唯一更改的是色标中的drop = F ,因此,即使没有与虚拟色阶相关的数据,它仍将显示在图例中,然后显示在图例标签中。

一个缺点是虚拟变量的图例键可见。 可能有一些方法可以消除这种情况,或者您可以通过为图例键赋予白色背景来使其不那么明显。

library(tidyverse)

set.seed(36)
Data <- data.frame(
    date = sample((as.Date(as.Date("2011-12-30"):as.Date("2012-01-04"),
                                                 origin="1970-01-01")), 1000, replace = TRUE),
    group = sample(c("0", "1"), 1000, replace = TRUE),
    outcome = sample(c("0", "1"), 1000, replace = TRUE))
Data %>%
    mutate(date = as.POSIXct(date), group = factor(group)) %>%
# add a dummy factor level & make it the first level
    mutate(group = group %>% fct_expand("dummy") %>% fct_relevel("dummy")) %>%
    group_by(group, date) %>% #group
    summarise(prop = sum(outcome == "1")/n()) %>% #calculate proportion 
    ggplot()+
    geom_line(aes(x = date, y = prop, color = group))+
    geom_point(aes(x = date, y = prop, color = group)) +
    scale_color_manual(values = c("0" = "blue", "1" = "black", "dummy" = "transparent"), labels = c("0" = "A", "1" = "B", "dummy" = "Some text that repeats,\nthe only unique thing being"), name = "Legend", drop = F)

reprex软件包 (v0.2.0)于2018-04-09创建。

暂无
暂无

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

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