使用 geom_label() 添加指向重要点的行

Add line pointing to important point with geom_label()

我正在制作折线图。我想通过添加一个文本标签来突出显示图表上的一个点,其中一条线指向线上的点。

使用 R sunspot.year 数据的可重现示例:

dat <- data_frame(NumSunSpots = sunspot.year, Year = c(1700:1988))

dat %>% 
ggplot(aes(Year, NumSunSpots))+
geom_line()+
geom_label(aes(x = 1952, y = 175, label ="Look at what is happening!"), vjust = 1, hjust = 1)

Image example

使用 geom_segment 作为简单的箭头:

使用 x、y xend 和 yend 调整到所需位置:

dat <- data_frame(NumSunSpots = sunspot.year, Year = c(1700:1988))

dat %>% 
    ggplot(aes(Year, NumSunSpots))+
    geom_line()+
    geom_label(aes(x = 1952, y = 175, label ="Look at what is happening!"), vjust = 1, hjust = 1) +
    geom_segment(aes(x = 1900, y = 250, xend = 1950, yend = 200),
                     arrow = arrow(length = unit(0.5, "cm")))