在 R 中绘制多个纵向轨迹时如何只标记一次?
How to label only once when plotting multiple longitudinal trajectories in R?
我画了一个多轨迹的图,就像图片中的那样https://i0.wp.com/svbtleusercontent.com/xcexi7wk8xsj1w_small.png?w=456&ssl=1
让我们将其用作可重现的示例:
library(ourworldindata)
id <- financing_healthcare %>%
filter(continent %in% c("Oceania", "Europe") & between(year, 2001, 2005)) %>%
select(continent, country, year, health_exp_total) %>%
na.omit()
ggplot(id, aes(x = year, y = health_exp_total, group = country, color = continent)) +
geom_line()
如果我想在我制作的情节中添加国家/地区的标签
ggplot(id, aes(x = year, y = health_exp_total, group = country, color = continent, label= country)) +
geom_line()+geom_text()
但因此,这些标签每年都会重复出现并与其他标签重叠。有没有可能每个标签只出现一年,避免重叠?
非常感谢!
#devtools::install_github('drsimonj/ourworldindata')
library(ourworldindata)
library(dplyr)
library(ggplot2)
library(ggrepel)
id <- financing_healthcare %>%
filter(continent %in% c("Oceania", "Europe") & between(year, 2001, 2005)) %>%
select(continent, country, year, health_exp_total) %>%
na.omit()
idl = id %>% filter(year == 2005)
ggplot(id, aes(x = year, y = health_exp_total, group = country, color = continent)) +
geom_line() +
geom_text_repel(data=idl, aes(label=country), size=2.5)
enter image description here
我画了一个多轨迹的图,就像图片中的那样https://i0.wp.com/svbtleusercontent.com/xcexi7wk8xsj1w_small.png?w=456&ssl=1 让我们将其用作可重现的示例:
library(ourworldindata)
id <- financing_healthcare %>%
filter(continent %in% c("Oceania", "Europe") & between(year, 2001, 2005)) %>%
select(continent, country, year, health_exp_total) %>%
na.omit()
ggplot(id, aes(x = year, y = health_exp_total, group = country, color = continent)) +
geom_line()
如果我想在我制作的情节中添加国家/地区的标签
ggplot(id, aes(x = year, y = health_exp_total, group = country, color = continent, label= country)) +
geom_line()+geom_text()
但因此,这些标签每年都会重复出现并与其他标签重叠。有没有可能每个标签只出现一年,避免重叠?
非常感谢!
#devtools::install_github('drsimonj/ourworldindata')
library(ourworldindata)
library(dplyr)
library(ggplot2)
library(ggrepel)
id <- financing_healthcare %>%
filter(continent %in% c("Oceania", "Europe") & between(year, 2001, 2005)) %>%
select(continent, country, year, health_exp_total) %>%
na.omit()
idl = id %>% filter(year == 2005)
ggplot(id, aes(x = year, y = health_exp_total, group = country, color = continent)) +
geom_line() +
geom_text_repel(data=idl, aes(label=country), size=2.5)
enter image description here