I'm creating a chart showing educational attainment trends. Here's the data:
structure(
list(
region_name = c("Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England", "Bedford", "CIPFA_average", "England"),
year = c("2018", "2018", "2018", "2019", "2019", "2019", "2020", "2020", "2020", "2021", "2021", "2021", "2022", "2022", "2022", "2023", "2023", "2023", "2018", "2018", "2018", "2019", "2019", "2019", "2020", "2020", "2020", "2021", "2021", "2021", "2022", "2022", "2022", "2023", "2023", "2023", "2018", "2018", "2018", "2019", "2019", "2019", "2020", "2020", "2020", "2021", "2021", "2021", "2022", "2022", "2022", "2023", "2023", "2023"),
age = c("Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Early Years", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Primary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary", "Secondary"),
gap_in_months = c(5.9, 4.51428571428571, 4.6, 6.1, 4.87857142857143, 4.6, NA, NA, NA, NA, NA, NA, 5.1, 5.275, 4.8, 5.3, 5.2375, 4.6, 11.5, 10.2642857142857, 9.2, 14.8, 10.9857142857143, 9.3, NA, NA, NA, NA, NA, NA, 13.3, 11.425, 10.3, 12.5, 11.775, 10.3, 18.4, 20.7357142857143, 18.1, 21, 20.6642857142857, 18.1, NA, NA, NA, NA, NA, NA, 20.1, 21.04375, 18.8, 21.5, 21.6, 19.2)
),
class = c("tbl_df", "tbl", "data.frame"),
row.names = c(NA, -54L)
)
And the code I'm using:
EPI_chart <- EPI_beds_CIPFA_eng %>%
ggplot(aes(x = year, y = gap_in_months, color = region_name, group = region_name)) +
geom_line(data = . %>% filter(!is.na(gap_in_months)), size = 1.5, linetype = "solid") +
geom_line(data = . %>% filter(is.na(gap_in_months)), size = 1.5, linetype = "dotted") +
facet_wrap(~ age, scales = "free_y") + theme_minimal() +
scale_color_manual(values = HT_colours_broad_3)
There are some NA values in the data, for the years 2020 and 2021. I want to fill these in with dashed lines. But my code is not doing this - I'm getting solid lines all the way. Is there any way of filling in the missing data with dotted lines?
There might be a more elegant solution, but I broke it up into three time periods (<2020, >2021, and 2019-2022) and created a geom_line
for each one of the three periods:
ggplot(EPI_beds_CIPFA_eng,
aes(x = year,
y = gap_in_months,
color = region_name,
group = region_name)) +
geom_line(data = . %>% filter(year < 2020), size = 1.5, linetype = "solid") +
geom_line(data = . %>% filter(year > 2021), size = 1.5, linetype = "solid") +
geom_line(data = . %>% filter(year %in% c(2019, 2022)), size = 1.5, linetype = "dotted") +
facet_wrap(~ age, scales = "free_y") +
theme_minimal()