在线图上标记程序标签以避免混淆线条 ggplot2

4
我想知道是否有一种方法可以在ggplot2的折线图上叠加标签,以避免穿过线条。我已经使用了vjust,在大多数情况下都有效,但是当两个日期之间有很大的增加或减少时,线会穿过标签,使其难以阅读。我将放置我当前使用的绘图和代码。在这种情况下,我想将920、1,467和1,480移离线条。我通过Reporters包将图导出到PowerPoint,所以我可以手动移动它,我只是想知道是否有一种避免这种情况的方法。
绘图: enter image description here 代码:
library("ggplot2")
library("scales")

line_data <- c(276, 475, 753, 840, 931, 962, 801, 920, 1467, 1840, 1737, 1638, 1789, 1733, 1480, 1464, 1538)
year_data <- c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
               2017)

line_data_total <- as.data.frame(cbind(line_data, year_data))

limit_func <- function(x) {
  if (nchar(max(x)) == 2){
    round(max(x +5), digits = -1)
  } else if (nchar(max(x)) == 3){
    round(max(x +50), digits = -2)
  } else if (nchar(max(x)) == 4){
    round(max(x +500), digits = -3)
  }
}

ggplot(data = line_data_total, aes(x = year_data, y = line_data, group = 1)) +
  geom_line(color = "red", size = 1.2)+
  geom_point(color = "red", fill = "red", shape = 23, size = 1.75) +
  geom_text(aes(label = paste0(format(round(as.numeric(line_data), 1), nsmall = 0, big.mark = ","))),
            size = 3, fontface = "bold", vjust = -2) +
  labs(x = '', y = '') +
  expand_limits(y = c(0, limit_func(line_data_total$line_data))) +
  scale_x_continuous(breaks = seq(min(line_data_total$year_data), max(line_data_total$year_data), 1)) +
  scale_y_continuous(labels = comma) +
  theme(panel.grid.major.x = element_blank() ,
        panel.grid.major.y = element_line( size=.1, color="light gray"),
        panel.background = element_rect(fill = "transparent"),
        plot.background = element_rect(fill = "transparent"),
        axis.text.x = element_text(face = "bold", size = 10),
        axis.text.y = element_text(face = "bold", size = 10),
        axis.ticks.y = element_blank())

如果其中一个答案解决了你的问题,你应该用勾号将其标记为最佳答案。 - Mako212
2个回答

3

使用 ggplot2 扩展包中的 ggrepel 怎么样:

require(ggrepel)

将你的 geom_text() 行替换为:

geom_text_repel(aes(label = paste0(format(round(as.numeric(line_data), 1), nsmall = 0, big.mark = ","))),
            size = 3, fontface = "bold", nudge_y=150)
nudge_y 是将标签从线上推开的参数,您可以使用 nudge_xnudge_y 的组合来获得更多控制。请查看软件包说明文档:https://github.com/slowkow/ggrepel/blob/master/vignettes/ggrepel.md 此外,上图是 示例图片

2

不需要更改 geom_text_repel 的设置,我想尝试一种适用于类似时间序列的通用解决方案。我编写了一个函数,它查看两个相邻点的 x 值,并计算它们之间的斜率以及中间点是否低于、高于或介于其x值相邻点之间的 y 方向。

  • 如果一个点的y值介于其x值相邻点之间,则标签将沿对它们连接的线的斜率对角线偏移。
  • 如果点y在其x值相邻点之下,则标签居中,但向下偏移。
  • 如果点y在其x值相邻点之上,则标签居中,但向上偏移。

library(dplyr)

adjust_away_from_line <- function(df, x, y, vextend = 0.5) {
  if(!is.data.frame(df)) {return(df)}

  x <- enquo(x)
  y <- enquo(y)

  if(!(quo_name(x) %in% names(df))) {
    warning(paste0("Column '", quo_name(x), "' not found in data."))
    return(df)
  }

  if(!(quo_name(y) %in% names(df))) {
    warning(paste0("Column '", quo_name(y), "' not found in data."))
    return(df)
  }


  df %>% arrange(!!x) %>% 
    mutate(nb.slope = case_when(
      is.na(lead(!!y)) ~ (    (!!y) - lag(!!y))/(    (!!x) - lag(!!x)),
      is.na(lag(!!y))  ~ (lead(!!y) -    (!!y))/(lead(!!x) -    (!!x)),
      TRUE             ~ (lead(!!y) - lag(!!y))/(lead(!!x) - lag(!!x))  
    ),
    nb.pos = case_when(
      is.na(lead(!!y))                              ~ -sign(nb.slope),
      is.na(lag(!!y))                               ~ -sign(nb.slope),
       (lead(!!y) >= (!!y)) &  (lag(!!y) >= (!!y))  ~  1.1,
      !(lead(!!y) >= (!!y)) & !(lag(!!y) >= (!!y))  ~ -1.1,
      TRUE                                          ~ -1
    ),
    hjust = case_when(
      nb.pos   >  1 ~ 0.5,
      nb.pos   < -1 ~ 0.5,
      nb.slope >  0 ~ 1,
      nb.slope <  0 ~ 0,
      TRUE          ~ 0.5
    ),
    vjust = scales::rescale(round(nb.pos), to = c(0-vextend, 1+vextend))) %>% 
    select(-nb.slope, -nb.pos)


}

由于该函数以数据框作为其第一个参数,因此您可以在管道中使用此函数,按顺序提供您的x变量和y变量的裸名称:

data.frame(line_data = c(276, 475, 753, 840, 931, 962, 801, 920, 1467, 
                         1840, 1737, 1638, 1789, 1733, 1480, 1464, 1538),
           year_data = c(2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
                         2011, 2012, 2013, 2014, 2015, 2016, 2017)
           ) %>%
  adjust_away_from_line(year_data, line_data) %>%
  ggplot(aes(year_data, line_data)) +
  geom_line() + 
  geom_point() +
  geom_text(aes(label = line_data, hjust = hjust, vjust = vjust))

在此输入图片描述

如果您想将标签从线条上向上或向下移动,可以使用adjust_away_from_line(..., vextend = ##)参数进行调整。默认值为0.5,但在不同的应用程序中,您可能需要0.75或1。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接