【问题标题】:ggplot2: how to add text to multiple vertical lines (geom_vlines) on a time x-axis?ggplot2:如何在时间 x 轴上将文本添加到多条垂直线(geom_vlines)?
【发布时间】:2017-05-05 01:18:33
【问题描述】:

请看下面的例子

library(dplyr)
library(lubridate)
library(ggplot2)
data <- data_frame(time = c(ymd(20160201),
                            ymd(20160202),
                            ymd(20160203),
                            ymd(20160201),
                            ymd(20160202)),
                            value = c(1,1,1,2,2), 
                            group = c('A','A','B','B','B'))

events <- data_frame(time = c(ymd(20160201), 
                              ymd(20160202)),
                     text = c('who let the dogs out?',
                              'who? who? who?'))

ggplot(data, aes(x = time, y = value, group = group, color = group)) + 
  geom_line(size = 2 ) +
  geom_vline(data = events, aes(xintercept = as.numeric(time)))  

> data
# A tibble: 5 × 3
        time value group
      <date> <dbl> <chr>
1 2016-02-01     1     A
2 2016-02-02     1     A
3 2016-02-03     1     B
4 2016-02-01     2     B
5 2016-02-02     2     B

> events
# A tibble: 2 × 2
        time                  text
      <date>                 <chr>
1 2016-02-01 who let the dogs out?
2 2016-02-02        who? who? who?

我想为每个组(A 和 B)获取变量 value 的折线图,并在 events 数据框中每次发生事件时绘制垂直线。

使用ggplot vertical line with date axisHow to get a vertical geom_vline to an x-axis of class date?How to add legend for vertical lines in ggplot? 的想法我可以轻松做到:

ggplot(data, aes(x = time, y = value, group = group, color = group)) + 
  geom_line(size = 2 ) +
  geom_vline(data = events, aes(xintercept = as.numeric(time)))

问题是我想用相应的文本标记每条垂直线(每个事件),如R ggplot2: Labelling a horizontal line on the y axis with a numeric value

很遗憾,执行以下操作不起作用

ggplot(data, aes(x = time, y = value, group = group, color = group)) + 
  geom_line(size = 2 ) +
  geom_vline(data = events, aes(xintercept = as.numeric(time)))  +
  geom_text(data = events, aes(x = as.numeric(time), y = 0, label = text))

这里有什么问题?有任何想法吗? 谢谢!

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    你可以试试

    ggplot(data, aes(x = time)) + 
      geom_line(aes(y = value, group = group, color = group), size = 2 ) +
      geom_vline(data = events, aes(xintercept = as.numeric(time)))  +
      geom_text(data = events, mapping = aes(label = text, y = 0), angle = 60, hjust = 0)
    

    【讨论】:

    • 我喜欢使用y = -Inf 来保持ylims 的响应。像这样geom_text(data = events, aes(label = text, x = time, y = -Inf), angle = 90, inherit.aes = F, hjust = -.5, vjust = -.5)
    • 是的,是的。 -inf 表示负无穷大。无论图表的 y 下限是什么,这都可以灵活地将标签放在底部。 (与y=0 相比,它静态地将其放置在0。)inherit.aes=FALSE 使geom_text 忘记了第一行中的全局aes(x = time)hjustvjust 进行水平和垂直对齐。 “这些可以是 0(右/下)和 1(上/左)之间的数字,也可以是字符(“左”、“中”、“右”、“下”、“中”、“上” “)。”
    • 您需要忘记group = group,因为变量events$group 不存在,而geom_text 正在寻找它。您可以改为设置group = NULLgeom_vline 不查找它,因此不会引发错误。
    • 还有Inf-Inf 的默认位置是视口的一半进/一半出,所以您需要h/v-just 舞蹈将其移出以阅读/查看
    • 也许 expand_limits geom_blank 会有所帮助,他们都做同样的事情
    猜你喜欢
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 2012-07-12
    • 2014-02-22
    • 2019-08-30
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多