如何增加情节的日期范围以便在系列结束时为 geom_text 腾出空间?
how to increase date range of a plot to make room for geom_text at the end of series?
我试图在最后使用 geom_text
为我的系列添加标签,为此我尝试增加 x 轴范围 为系列腾出空间标签,但它不会增加 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)
使用:
scale_x_date(limits = c(as.Date("2021-03-01"),max(rtpcr_test_daily_pct$Updated.On) + 15))
完整代码-
library(tidyverse)
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(limits = c(as.Date("2021-03-01"),max(rtpcr_test_daily_pct$Updated.On) + 15)) +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "% RTPCR testing Between Karnataka & Delhi- Mar'21 onwards") +
coord_equal(ratio = 70)
我试图在最后使用 geom_text
为我的系列添加标签,为此我尝试增加 x 轴范围 为系列腾出空间标签,但它不会增加 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)
使用:
scale_x_date(limits = c(as.Date("2021-03-01"),max(rtpcr_test_daily_pct$Updated.On) + 15))
完整代码-
library(tidyverse)
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(limits = c(as.Date("2021-03-01"),max(rtpcr_test_daily_pct$Updated.On) + 15)) +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "% RTPCR testing Between Karnataka & Delhi- Mar'21 onwards") +
coord_equal(ratio = 70)