当一组是单条黑线而另一组是多条彩色线时,如何连接ggplot2中的两组线?

问题描述 投票:0回答:1

我正在尝试创建一个如下所示的图(Powerpoint 中的模型): Powerpoint mock-up

不幸的是,我只能得到这个(A): Figure A

或者这个(B): Figure B

或者这个(C): Figure C 我尝试过的:

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) 最接近,但我只需要防止彩色路径彼此闭合。任何帮助将不胜感激!

r ggplot2 path line group
1个回答
0
投票

您需要一条额外的路径来连接这两个类别。您可以使用新的

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")

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.