R语言 如何为具有两个y轴和两个数据集的geom_path图添加图例?

uhry853o  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(185)

我有这个图,有两个y轴,包括两个数据集,但我想为四条线添加一个图例:Vertical plot
我有以下代码:

codbayplot1 <- ggplot(codbay, aes(y = Depth, x = Temp))+
  geom_path(aes(x = Temp), color = "red")+
  geom_path(aes(x = Sal-34.25), color = "blue")+
  scale_y_reverse(name = "Depth [m]")+
  scale_x_continuous(name = "Temperature [°C]",sec.axis=sec_axis(~34.25+ .,name = "Salinity [PSU]"))+
  theme(
    axis.title.x.top = element_text(color = "blue"),
    axis.title.x.bottom = element_text(color = "red"))+
  geom_path(data=frontAS, color="red", aes(x=Temp, y=Depth), linetype = "dashed")+
  geom_path(data=frontAS, color="blue", aes(x = Sal-34.25), linetype = "dashed")+
  scale_x_continuous(name = "Temperature [°C]",sec.axis=sec_axis(~34.25+ .,name = "Salinity [PSU]"))

print(codbayplot1)

我有以下两个数据集(我不知道你是否可以像这样使用数据集,但它们来自csv文件,所以我不知道如何共享它们-如果我应该做其他事情,请让我知道):
科迪贝:

Depth  Temp  Psal Density Cond   Sal
1      3 -1.62 33.04    26.6 26.2 33.04
2      4 -1.61 33.03    26.6 26.2 33.03
3      5 -1.61 33.03    26.6 26.2 33.03
4      6 -1.61 33.03    26.6 26.3 33.03
5      7 -1.60 33.03    26.6 26.3 33.03
6      8 -1.59 33.04    26.6 26.3 33.04
7      9 -1.59 33.04    26.6 26.3 33.04
8     10 -1.59 33.04    26.6 26.3 33.04
9     11 -1.59 33.04    26.6 26.3 33.04
10    12 -1.59 33.05    26.6 26.3 33.05
11    13 -1.59 33.05    26.7 26.3 33.05
12    14 -1.59 33.05    26.7 26.3 33.05
13    15 -1.59 33.06    26.7 26.3 33.06
14    16 -1.59 33.06    26.7 26.3 33.06
15    17 -1.59 33.06    26.7 26.3 33.06
16    18 -1.59 33.06    26.7 26.3 33.06
17    19 -1.59 33.07    26.7 26.3 33.07
18    20 -1.59 33.07    26.7 26.3 33.07
19    21 -1.59 33.07    26.7 26.3 33.07
20    22 -1.59 33.07    26.7 26.3 33.08
21    23 -1.59 33.08    26.7 26.3 33.08
22    24 -1.59 33.08    26.7 26.3 33.08
23    25 -1.59 33.10    26.8 26.3 33.10
24    26 -1.59 33.09    26.8 26.3 33.09
25    27 -1.58 33.10    26.8 26.3 33.10
26    28 -1.58 33.10    26.8 26.3 33.10
27    29 -1.58 33.10    26.8 26.3 33.10
28    30 -1.58 33.11    26.8 26.3 33.11
29    31 -1.57 33.11    26.8 26.3 33.11
30    32 -1.57 33.11    26.8 26.3 33.11
31    33 -1.57 33.11    26.8 26.4 33.11
32    34 -1.56 33.12    26.8 26.4 33.12
33    35 -1.57 33.12    26.8 26.4 33.12
34    36 -1.57 33.12    26.8 26.4 33.12
35    37 -1.57 33.12    26.8 26.4 33.12
36    38 -1.57 33.12    26.8 26.4 33.12
37    39 -1.57 33.12    26.8 26.4 33.12
38    40 -1.56 33.13    26.8 26.4 33.13
39    41 -1.56 33.13    26.9 26.4 33.13
40    42 -1.56 33.13    26.9 26.4 33.13
41    43 -1.56 33.13    26.9 26.4 33.13
42    44 -1.56 33.13    26.9 26.4 33.13
43    45 -1.56 33.13    26.9 26.4 33.13
44    46 -1.56 33.13    26.9 26.4 33.13
45    47 -1.56 33.14    26.9 26.4 33.14
46    48 -1.55 33.14    26.9 26.4 33.14
47    49 -1.55 33.14    26.9 26.4 33.14
48    50 -1.55 33.14    26.9 26.4 33.14
49    51 -1.54 33.14    26.9 26.4 33.14
50    52 -1.53 33.15    26.9 26.4 33.15
51    53 -1.49 33.15    26.9 26.5 33.15
52    54 -1.47 33.16    26.9 26.5 33.16
53    55 -1.45 33.16    26.9 26.5 33.16
54    56 -1.37 33.18    27.0 26.6 33.18
55    57 -1.34 33.17    27.0 26.6 33.17
56    58 -1.32 33.18    27.0 26.6 33.18
57    59 -1.31 33.18    27.0 26.6 33.18
58    60 -1.29 33.18    27.0 26.6 33.18
59    61 -1.28 33.18    27.0 26.7 33.19
60    62 -1.27 33.19    27.0 26.7 33.19
61    63 -1.26 33.19    27.0 26.7 33.19
62    64 -1.21 33.20    27.0 26.7 33.20
63    65 -1.13 33.21    27.0 26.8 33.21
64    66 -1.10 33.21    27.0 26.8 33.21
65    67 -1.06 33.22    27.0 26.9 33.22
66    68 -1.02 33.24    27.1 26.9 33.24
67    69 -0.88 33.26    27.1 27.0 33.26
68    70 -0.76 33.28    27.1 27.1 33.28
69    71 -0.64 33.28    27.1 27.2 33.29
70    72 -0.61 33.29    27.1 27.3 33.28
71    73 -0.58 33.29    27.1 27.3 33.30
72    74 -0.52 33.31    27.1 27.4 33.31
73    75 -0.49 33.30    27.1 27.4 33.30
74    76 -0.47 33.31    27.1 27.4 33.31
75    77 -0.46 33.31    27.1 27.4 33.31
76    78 -0.45 33.31    27.1 27.4 33.31
77    79 -0.45 33.31    27.1 27.4 33.31
78    80 -0.46 33.31    27.1 27.4 33.31
79    81 -0.46 33.31    27.2 27.4 33.31
80    82   NaN   NaN     NaN  NaN   NaN

和frontAS

Depth  Temp Cond Psal Density Press  Sal
1      3 -1.68 2.63 33.2    1030  5.08 33.2
2      4 -1.69 2.63 33.2    1030  6.80 33.2
3      5 -1.68 2.63 33.2    1030  8.01 33.2
4      6 -1.68 2.63 33.2    1030  9.50 33.2
5      7 -1.69 2.63 33.2    1030 10.90 33.2
6      8 -1.70 2.63 33.2    1030 12.60 33.2
7      9 -1.70 2.63 33.2    1030 13.90 33.2
8     10 -1.69 2.63 33.2    1030 15.40 33.2
9     11 -1.69 2.63 33.2    1030 16.80 33.2
10    12 -1.69 2.63 33.2    1030 18.40 33.2
11    13 -1.69 2.63 33.2    1030 19.80 33.2
12    14 -1.68 2.63 33.2    1030 21.30 33.2
13    15 -1.68 2.63 33.2    1030 22.70 33.2
14    16 -1.69 2.63 33.2    1030 24.20 33.2
15    17 -1.69 2.63 33.2    1030 25.60 33.2
16    18 -1.68 2.63 33.2    1030 27.10 33.2
17    19 -1.68 2.63 33.2    1030 28.50 33.2
18    20 -1.68 2.63 33.2    1030 29.90 33.2
19    21 -1.69 2.63 33.2    1030 31.50 33.2
20    22 -1.68 2.63 33.2    1030 33.00 33.2
21    23 -1.68 2.63 33.2    1030 34.40 33.2
22    24 -1.69 2.63 33.2    1030 36.00 33.2
23    25 -1.68 2.63 33.2    1030 37.40 33.2
24    26 -1.68 2.63 33.2    1030 38.80 33.2
25    27 -1.68 2.63 33.2    1030 40.30 33.2
26    28 -1.68 2.63 33.2    1030 41.80 33.2
27    29 -1.68 2.63 33.2    1030 43.20 33.2
28    30 -1.68 2.63 33.2    1030 44.60 33.2
29    31 -1.68 2.63 33.2    1030 46.10 33.2
30    32 -1.68 2.63 33.2    1030 47.60 33.2
31    33 -1.68 2.63 33.2    1030 49.10 33.2
32    34 -1.68 2.63 33.2    1030 50.60 33.2
33    35 -1.68 2.63 33.2    1030 52.00 33.2
34    36 -1.68 2.63 33.2    1030 53.40 33.2
35    37 -1.68 2.63 33.2    1030 54.80 33.2
36    38 -1.68 2.63 33.2    1030 56.30 33.2
37    39 -1.68 2.63 33.2    1030 57.80 33.2
38    40 -1.68 2.63 33.2    1030 59.30 33.2
39    41 -1.68 2.63 33.2    1030 60.80 33.2
40    42 -1.68 2.63 33.2    1030 62.20 33.2
41    43 -1.67 2.63 33.2    1030 63.70 33.2
42    44 -1.67 2.63 33.2    1030 65.20 33.2
43    45 -1.67 2.63 33.2    1030 66.60 33.2
44    46 -1.67 2.63 33.2    1030 68.20 33.2
45    47 -1.67 2.63 33.2    1030 69.80 33.2
46    48 -1.67 2.63 33.2    1030 71.00 33.2
47    49 -1.67 2.63 33.2    1030 72.50 33.2
48    50 -1.66 2.64 33.2    1030 74.00 33.2
49    51 -1.66 2.64 33.2    1030 75.30 33.2
50    52 -1.66 2.64 33.2    1030 77.10 33.2
51    53 -1.66 2.64 33.2    1030 78.30 33.2
52    54 -1.65 2.64 33.2    1030 79.80 33.2
53    55 -1.65 2.64 33.2    1030 81.40 33.2
54    56 -1.65 2.64 33.2    1030 82.80 33.2
55    57 -1.64 2.64 33.2    1030 84.40 33.2
56    58 -1.64 2.64 33.2    1030 85.80 33.2
57    59 -1.62 2.64 33.2    1030 87.40 33.2

我试过用scale_color_manual添加图例,但没有成功:

codbayplot2 <- ggplot(codbay, aes(y = Depth, x = Temp))+
  geom_path(aes(x = Temp), color = "Site1_T")+
  geom_path(aes(x = Sal-34.25), color = "Site1_S")+
  scale_y_reverse(name = "Depth [m]")+
  scale_x_continuous(name = "Temperature [°C]",sec.axis=sec_axis(~34.25+ .,name = "Salinity [PSU]"))+
  theme(
    axis.title.x.top = element_text(color = "blue"),
    axis.title.x.bottom = element_text(color = "red"))+
  geom_path(data=frontAS, color="red", aes(x=Temp, y=Depth, color = "Site2_T"), linetype = "dashed")+
  geom_path(data=frontAS, color="blue", aes(x = Sal-34.25, color = "Site2_S"), linetype = "dashed")+
  scale_x_continuous(name = "Temperature [°C]",sec.axis=sec_axis(~34.25+ .,name = "Salinity [PSU]"))+
  scale_color_manual(name = "", values = c( "Site1_T"= "red", "Site1_S" = "blue","Site2_T"= "red", "Site2_S" = "blue"))

print(codbayplot2)
gcuhipw9

gcuhipw91#

问题是,要获得图例,您必须在美学上进行Map,即。在aes内部移动color=...。否则scale_color_manual将不会有任何效果。此外,您可以通过将两个数据集绑定到一个数据集来简化代码。但是,在本例中,我们还需要在linetype aes上进行Map,以反映不同的线型。除此之外,进一步简化代码的另一个步骤是重塑数据集。

library(ggplot2)
library(dplyr)

dat <- list(
  "Site1" = codbay,
  "Site2" = frontAS
) |>
  dplyr::bind_rows(.id = "site")

ggplot(dat, aes(y = Depth, x = Temp)) +
  geom_path(aes(
    x = Temp,
    color = paste(site, "T", sep = "_"),
    linetype = paste(site, "T", sep = "_")
  )) +
  geom_path(aes(
    x = Sal - 34.25,
    color = paste(site, "S", sep = "_"),
    linetype = paste(site, "S", sep = "_")
  )) +
  scale_y_reverse(name = "Depth [m]") +
  scale_x_continuous(
    name = "Temperature [°C]", 
    sec.axis = sec_axis(~ 34.25 + ., name = "Salinity [PSU]")
  ) +
  scale_color_manual(
    values = c(
      Site1_T = "red", Site1_S = "blue",
      Site2_T = "red", Site2_S = "blue"
    )
  ) +
  scale_linetype_manual(
    values = c(
      Site1_T = "solid", Site1_S = "solid",
      Site2_T = "dashed", Site2_S = "dashed"
    )
  ) +
  theme(
    axis.title.x.top = element_text(color = "blue"),
    axis.title.x.bottom = element_text(color = "red")
  ) +
  labs(color = NULL, linetype = NULL)

相关问题