如何增加绘图的日期范围以在系列结束时为 geom_text 腾出空间?

问题描述

我正在尝试使用 geom_text 在最后为我的系列添加标签,为此我尝试增加 x 轴范围 为系列标签腾出空间,但它没有' t 用 scale_x_date 增加它的范围。

数据

library(tidyverse)
library(lubridate)
library(scales)

file_url1 <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/rtpcr_test_daily_pct.csv"

rtpcr_test_daily_pct <- read.csv(url(file_url1))

rtpcr_test_daily_pct <- rtpcr_test_daily_pct %>%
  mutate(Updated.On = as.Date(Updated.On))

以下是我尝试过的代码,无论我使用 scale_x_date() 向 x 轴日期添加什么值,x 轴框架都保持不变。

我不确定我在下面尝试过的代码有什么问题:

rtpcr_test_daily_pct %>% 
  
  filter(!is.na(pct_rtpcr),pct_rtpcr > 0 ) %>% 
  
  ggplot(aes(x = Updated.On,y = pct_rtpcr,col = State) 
         ) +
  geom_line(size = 1) +
  
  geom_text(data = rtpcr_test_daily_pct %>% 
              filter(Updated.On == max(Updated.On)-1),aes(label = State,x = Updated.On,y = pct_rtpcr ),vjust = -1,size = 3) +
  
  scale_y_continuous(labels = percent,breaks = seq(.1,1,by = .1)) +
  
  expand_limits(y = .1 ) + # 
  scale_x_date(aes(limits = as.Date(c("2021-03-01",max(Updated.On) + 15)))) +
  
  theme_minimal() +
  theme(legend.position = "none") +
  
  labs(title = "% RTPCR testing Between Karnataka & Delhi- Mar'21 onwards") +
  coord_equal(ratio = 70)

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)