To preface, I'm relatively new to R and trying to replicate a previous user's inquiry found here: Stackoverflow question.
Instead of the data being in days, and looking only at more recent set of dates, I want to include all points of time, in quarters, for the data. I keep encountering issues as I'm not well equipped to understand each point of their code in order to go about this in a reasonable way.
Edit: I've tried to follow the example, however, I still can't replicate the example provided. I'm trying to get the labels to the right, and change the x-axis to display Q1 2015, Q2 2015, etc. I've placed my attempt at the code below:
library(readxl)
library(ggrepel)
library(tidyverse)
library(ggplot2)
owid <- read_xlsx("/Desktop/testR.xlsx") %>%
filter(date >= "2014-01-01" & date <= "2020-07-01") %>%
select(location, date, outcome) %>%
arrange(location, date) %>%
group_by(location) %>%
complete(date = seq.Date(as.Date("2014-01-01"),
as.Date("2020-07-01"),
by="quarter")) %>%
fill(outcome) %>%
ungroup() %>%
mutate(location = factor(location),
location = fct_reorder2(location, outcome,
outcome)) %>%
mutate(datenew= as.Date(date, format= "%d.%m.%Y")) %>%
mutate(label = if_else(datenew == max(datenew),
as.character(location),
NA_character_)) %>%
mutate(yq = as.yearqtr(datenew))
G01 <-
owid %>%
ggplot(aes(x=datenew, y=outcome, group=location,
color=location)) +
geom_point() +
geom_line() +
theme_minimal() +
labs(y="",
x="") +
theme(panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(linetype = "dashed"),
panel.grid.minor.y = element_blank(),
panel.grid.minor.x = element_blank(),
plot.title.position = "plot",
plot.title = element_text(face="bold"),
legend.position = "none") +
scale_y_continuous(breaks=c(seq(0, 70, 10))) +
scale_x_date(breaks = as.Date(c("2015-01-01",
"2015-04-01",
"2015-07-01",
"2015-10-01",
"2016-01-01",
"2016-04-01",
"2016-07-01",
"2016-10-01",
"2017-01-01",
"2017-04-01",
"2017-07-01",
"2017-10-01",
"2018-01-01",
"2018-04-01",
"2018-07-01",
"2018-10-01",
"2019-01-01",
"2019-04-01",
"2019-07-01",
"2019-10-01",
"2020-01-01",
"2020-04-01",
"2020-07-01")),
labels = scales::date_format("%Y-%m"),
limits = as.Date(c("2015-01-01",
"2020-07-01")))
G01 +
geom_text_repel(aes(label = gsub("^.*$", " ", label)), # This will force the correct position of the link's right end.
segment.curvature = -0.1,
segment.square = TRUE,
segment.color = 'grey',
box.padding = 0.1,
point.padding = 0.6,
nudge_x = 0.15,
nudge_y = 1,
force = 0.5,
hjust = 0,
direction="y",
na.rm = TRUE,
xlim = as.Date(c("2015-01-01", "2020-07-01")),
ylim = c(0,70),
) +
geom_text_repel(data = . %>% filter(!is.na(label)),
aes(label = paste0(" ", label)),
segment.alpha = 0, ## This will 'hide' the link
segment.curvature = -0.1,
segment.square = TRUE,
# segment.color = 'grey',
box.padding = 0.1,
point.padding = 0.6,
nudge_x = 0.15,
nudge_y = 1,
force = 0.5,
hjust = 0,
direction="y",
na.rm = TRUE,
xlim = as.Date(c("2015-01-01", "2020-07-01")),
ylim = c(0,70))
My results look like such here