我正在尝试创建一个如下所示的图(Powerpoint 中的模型):
data
scenario years value dummy
1 Normals 1961-1990 0.0000000 Normal
2 Normals 1991-2020 0.1965065 Normal
3 SSP126 2011-2040 0.1366428 Normal
4 SSP126 2041-2070 0.1727662 Normal
5 SSP126 2071-2100 0.1866775 Normal
6 SSP245 2011-2040 0.1334094 Normal
7 SSP245 2041-2070 0.1865576 Normal
8 SSP245 2071-2100 0.2203371 Normal
9 SSP370 2011-2040 0.1315721 Normal
10 SSP370 2041-2070 0.2000836 Normal
11 SSP370 2071-2100 0.2675299 Normal
12 SSP585 2011-2040 0.1351562 Normal
13 SSP585 2041-2070 0.2060075 Normal
14 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))
此代码片段生成图 (A)。包含
group = dummy
会生成图 (B),删除开头的 arrange(years)
会生成图 (C)。正如你所看到的,我尝试对年份进行重新排序,这样我就不会像图 (C) 中那样使路径相互闭合。我认为 (C) 最接近,但我只需要防止彩色路径彼此闭合。任何帮助将不胜感激!
您需要一条额外的路径来连接这两个类别。您可以使用新的
data
层的
geom_path
参数即时执行此操作
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"))) +
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")