停止 ggplot 在 na 值上绘制线条

问题描述 投票:0回答:1

我正在创建一个凹凸图来显示 10 年内前 10 个值的排名变化。当某些东西跌出前 10 名时,我会保留它下降到的值,以显示该项目的排名下降了多少。然而,如果它重新进入前十,那么我希望它能在两者之间有所突破。然而目前它看起来像这样:

enter image description here

这是我目前用来生成此图表的函数:

ggplot(df, aes(x = year, y = rank_pos, color = category)) +
    geom_point(size=8) +
    geom_bump(size=1.5) +
    geom_text(aes(label = rank), size = 6,color = "white") +
    geom_text(data=start_data, aes(x = year-0.5, y = rank, label = category), hjust="right",colour="black",size = 5)+
    geom_text(data=end_data, aes(x = year+0.5, y = rank, label = category), hjust="left",colour="black",size = 5)+
    geom_text(aes(label = ifelse(rank == 1, year, "")), size = 4, hjust = 0.5, vjust = -3,colour="black")+
    scale_colour_manual(values = colours) +
    theme_minimal() +
    theme(legend.position = "none",
          axis.title.y = element_blank(),
          axis.text.y = element_blank(),
          axis.title.x = element_text(vjust = 1, size=13),
          axis.text.x = element_blank(),
          panel.grid.major.y = element_blank(),
          panel.grid.minor.y = element_blank())+
    scale_y_continuous(trans = "reverse", breaks = unique(df$rank),expand=c(0.1,0.0)) +
    scale_x_continuous(expand=c(0.25,0.25),position = "top") +
    xlab("Year")

我要调整的排名位置,但我需要知道如何创建一个休息点,例如黑人排名垫底的需要2014年和2020年之间的差距。

预计到达时间:

structure(list(category = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L), levels = c("A", "B", "C", "D", "E", 
"F", "G", "H", "I", "J", "K", "L"), class = "factor"), year = c(2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022), 
    rank = c(9L, 7L, 7L, 8L, 8L, 7L, 10L, 8L, 9L, 5L, 6L, 12L, 
    10L, 10L, 9L, 6L, 5L, 3L, 3L, 3L, 3L, 3L, 6L, 5L, 4L, 4L, 
    4L, 4L, 5L, 6L, 6L, 7L, 5L, 4L, 4L, 6L, 6L, 9L, 9L, 8L, 9L, 
    10L, 10L, 11L, 3L, 3L, 5L, 5L, 5L, 6L, 6L, 5L, 5L, 6L, 8L, 
    7L, 9L, 8L, 7L, 7L, 8L, 7L, 7L, 8L, 8L, 7L, NA, NA, NA, NA, 
    NA, NA, NA, 19L, 7L, 9L, 9L, 8L, 8L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 10L, 11L, NA, NA, NA, NA, NA, NA, NA, 11L, 
    10L, 5L, 6L, 9L, 10L, 10L, 10L, 9L, 10L, 11L, NA, NA, 2L, 
    2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L), rank_pos = c(9, 7, 7, 8, 8, 7, 10, 
    8, 9, 5, 6, 11, 10, 10, 9, 6, 5, 3, 3, 3, 3, 3, 6, 5, 4, 
    4, 4, 4, 5, 6, 6, 7, 5, 4, 4, 6, 6, 9, 9, 8, 9, 10, 10, 11, 
    3, 3, 5, 5, 5, 6, 6, 5, 5, 6, 8, 7, 9, 8, 7, 7, 8, 7, 7, 
    8, 8, 7, NA, NA, NA, NA, NA, NA, NA, 11, 7, 9, 9, 8, 8, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 10, 11, NA, NA, NA, NA, NA, NA, NA, 
    11, 10, 5, 6, 9, 10, 10, 10, 9, 10, 11, NA, NA, 2, 2, 3, 
    3, 3, 3, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -132L), groups = structure(list(
    category= structure(1:12, levels = c("A", "B", "C", "D", 
    "E", "F", "G", "H", "I", "J", "K", "L"), class = "factor"), 
    .rows = structure(list(1:11, 12:22, 23:33, 34:44, 45:55, 
        56:66, 67:77, 78:88, 89:99, 100:110, 111:121, 122:132), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))
r ggplot2
1个回答
0
投票

一种可能的方法是将间隙的

rank_pos
设置为
12
,然后

  1. 使用
    coord_cartesian()
    仅“放大”您想要可见的部分
  2. 或使用
    annotate("rect", ymin = 11.5, ymax = 12.5, xmin = 2011, xmax = 2023, fill = "white")
    隐藏 12 的
    rank_pos
library(tidyverse)
library(ggbump)

df |> 
  mutate(rank_pos = replace_na(rank_pos, 12)) |> 
  ggplot(aes(x = year, y = rank_pos, color = category)) +
  geom_point(size = 8) +
  geom_bump(size = 1.5) +
  geom_text(aes(label = rank), size = 6, color = "white") +
  # geom_text(data=start_data, aes(x = year-0.5, y = rank, label = category), hjust="right",colour="black",size = 5)+
  # geom_text(data=end_data, aes(x = year+0.5, y = rank, label = category), hjust="left",colour="black",size = 5)+
  geom_text(aes(label = ifelse(rank == 1, year, "")), size = 4, hjust = 0.5, vjust = -3, colour = "black") +
  # scale_colour_manual(values = colours) +
  theme_minimal() +
  theme(
    legend.position = "none",
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.title.x = element_text(vjust = 1, size = 13),
    axis.text.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank()
  ) +
  scale_y_continuous(trans = "reverse", breaks = unique(df$rank_pos), expand = c(0.1, 0.0)) +
  scale_x_continuous(expand = c(0.25, 0.25), position = "top") +
  xlab("Year") +
  coord_cartesian(ylim = c(10.5, 0))

创建于 2024-05-02,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.