I am trying to create a plot that looks like this (mockup in Powerpoint):
Unfortunately, I can only manage to get this (A):
data < - examp = tribble(~scenario, ~years, ~value, ~dummy,
'Normals',' 1961-1990', 0.0000000, 'Normal',
'Normals',' 1961-1990', 0.0000000, 'Normal',
'Normals',' 1991-2020', 0.1965065, 'Normal',
'SSP126 ','2011-2040 ', 0.1366428 , 'Normal',
'SSP126 ','2041-2070 ', 0.1727662 , 'Normal',
'SSP126 ','2071-2100 ', 0.1866775 , 'Normal',
'SSP245 ','2011-2040 ', 0.1334094 , 'Normal',
'SSP245 ','2041-2070 ', 0.1865576 , 'Normal',
'SSP245 ','2071-2100 ', 0.2203371 , 'Normal',
'SSP370 ','2011-2040 ', 0.1315721 , 'Normal',
'SSP370 ','2041-2070 ', 0.2000836 , 'Normal',
'SSP370 ','2071-2100 ', 0.2675299 , 'Normal',
'SSP585 ','2011-2040 ', 0.1351562 , 'Normal',
'SSP585 ','2041-2070 ', 0.2060075 , 'Normal',
'SSP585 ','2071-2100 ', 0.3025003 , 'Normal')
data %>%
arrange(years) %>%
mutate(scenario = fct_reorder(scenario, value)) %>%
ggplot(aes(x = years, y = value)) +
geom_path(aes(group = scenario, colour = scenario), linewidth = 1) +
scale_colour_manual(name = "Climate scenario",
values = col,
breaks = c("Normals", "Normals", "SSP126", "SSP245", "SSP370", "SSP585"),
labels = c("Normals", "", "SSP126", "SSP245", "SSP370", "SSP585")) +
scale_x_discrete(name = "Years",
breaks = c("1961-1990", "1991-2020", "2011-2040", "2041-2070", "2071-2100"),
labels = c("1961-1990", "1991-2020", "2011-2040", "2041-2070", "2071-2100")) +
theme(axis.text.x = element_text(angle = 35, hjust = 1))
This code snippet produces plot (A). Including group = dummy
produces plot (B), and removing arrange(years)
at the beginning produces plot (C). As you can see I have tried reordering the years so that I don't get the paths closing on each other, like in plot (C). I think (C) is closest, but I just need to prevent the coloured paths from closing on each other. Any assistance would be much appreciated!
You need an extra path that joins the two x axis categories "1991-2020" an "2011-2040". You can do this on the fly using the data
argument of a new geom_path
layer
col <- c("black", "black", "#1e73b5", "#e3923d", "#328e5c", "#7b78b2")
data %>%
arrange(years) %>%
mutate(scenario = fct_reorder(scenario, value)) %>%
ggplot(aes(x = years, y = value)) +
geom_path(aes(group = scenario, colour = scenario), linewidth = 1) +
geom_path(aes(group = 1), colour = "black", linewidth = 1,
data = . %>% filter(years %in% c("1991-2020", "2011-2040")) %>%
group_by(years) %>% summarise(value = mean(value))) +
scale_colour_manual(name = "Climate scenario",
values = col,
breaks = c("Normals", "Normals", "SSP126",
"SSP245", "SSP370", "SSP585"),
labels = c("Normals", "", "SSP126", "SSP245",
"SSP370", "SSP585")) +
scale_x_discrete(name = "Years",
breaks = c("1961-1990", "1991-2020", "2011-2040",
"2041-2070", "2071-2100"),
labels = c("1961-1990", "1991-2020", "2011-2040",
"2041-2070", "2071-2100")) +
theme_classic() +
theme(axis.text.x = element_text(angle = 35, hjust = 1),
panel.background = element_rect(fill = "white", color = "black"),
legend.position = "bottom")