如何在 ggplot2 中的 X 和 Y 下绘制多列

How can I plot multiple columns under X and Y in ggplot2

data <- structure(list(A_w = c(0, 0.69, 1.41, 2.89, 6.42, 13.3, 25.5, 
  36.7, 44.3, 46.4), E_w = c(1.2, 1.2, 1.5, 1.6, 1.9, 2.3, 3.4, 
    4.4, 10.6, 16.5), A_e = c(0, 0.18, 0.37, 0.79, 1.93, 4.82, 11.4, 
      21.6, 31.1, 36.2), E_e = c(99.4, 99.3, 98.9, 98.4, 97.1, 93.3, 
        84.7, 71.5, 58.1, 48.7)), row.names = c(NA, -10L), class = "data.frame")
data
#>      A_w  E_w   A_e  E_e
#> 1   0.00  1.2  0.00 99.4
#> 2   0.69  1.2  0.18 99.3
#> 3   1.41  1.5  0.37 98.9
#> 4   2.89  1.6  0.79 98.4
#> 5   6.42  1.9  1.93 97.1
#> 6  13.30  2.3  4.82 93.3
#> 7  25.50  3.4 11.40 84.7
#> 8  36.70  4.4 21.60 71.5
#> 9  44.30 10.6 31.10 58.1
#> 10 46.40 16.5 36.20 48.7

reprex package (v2.0.0)

于 2021-05-31 创建

我正在尝试使用所有 A 值作为 X 和 Es 作为 Y 来绘制此数据。我如何将 a) 这两列绘制在 ggplot2 上,或 b) 重新排列此数据框以组合 A 列和 E 列到最终数据帧中,只有两列,行数是图中的 2 倍?

感谢您的帮助,我是初学者(显然)

为清晰起见编辑:重要的是 A_e 和 E_e 值保持成对,类似于 A_w 和 E_w 值保持成对。最终结果图应该类似于此图像的橙色和蓝色线条,但我在学习 R 时试图复制它。

目前,当分成两个 2x10 的数据帧时,我能够分别绘制每个数据帧

     A_w  E_w
1   0.00  1.2
2   0.69  1.2
3   1.41  1.5
4   2.89  1.6
5   6.42  1.9
6  13.30  2.3
7  25.50  3.4
8  36.70  4.4
9  44.30 10.6
10 46.40 16.5

和第二个情节

# A tibble: 10 x 2
     A_e   E_e
   <dbl> <dbl>
 1  0     99.4
 2  0.18  99.3
 3  0.37  98.9
 4  0.79  98.4
 5  1.93  97.1
 6  4.82  93.3
 7 11.4   84.7
 8 21.6   71.5
 9 31.1   58.1
10 36.2   48.7

但我的最终目标是让它们都在同一个图上,就像上面的 Excel 图(橙色 + 蓝色图)一样。

“手工”完成:

#dummmy data:
df = data.frame(A_w=rnorm(10), E_w=rnorm(10), A_e=rnorm(10), E_e=rnorm(10))

df2 = data.frame(A=c(df$A_w, df$A_e), E=c(df$E_w, df$A_e))

输出:

> df2
             A          E
1   1.25522468 -0.2441768
2  -0.50585191 -0.1383637
3   0.42374270 -0.9664189
4  -0.39858532 -0.3442157
5  -1.05665363 -1.3574362
6   0.79191788 -0.8202841
7  -1.31349592  0.7280619
8  -0.05609851  0.6365495
9   1.01068811  2.0222241
10 -1.15572972 -0.2190794
11  0.15579931  0.1557993
12  1.58834329  1.5883433
13  1.24933622  1.2493362
14 -0.28197439 -0.2819744
15  0.30593184  0.3059318
16  0.75486103  0.7548610
17  1.19394302  1.1939430
18 -1.79955846 -1.7995585
19  0.59688655  0.5968865
20  0.71519048  0.7151905

对于情节:ggplot(df2, aes(x=A, y=E)) + geom_point()

输出:

有一些方法可以做到这一点,而不必通过列出他们的名字来连接列 - 使用 tidyr 包 - 但我认为这个解决方案更容易从初学者的角度理解。

你可以试试这个

data <- data.frame(
                 A_w = c(0,0.69,1.41,2.89,6.42,
                         13.3,25.5,36.7,44.3,46.4),
         E_w = c(1.2, 1.2, 1.5, 1.6, 1.9, 2.3, 3.4, 4.4, 10.6, 16.5),
                 A_e = c(0,0.18,0.37,0.79,1.93,
                         4.82,11.4,21.6,31.1,36.2),
                 E_e = c(99.4,99.3,98.9,98.4,
                         97.1,93.3,84.7,71.4,58.1,48.7)
        )

library(tidyverse)
data %>% pivot_longer(everything(), names_sep = '_', names_to = c('.value', 'type')) %>%
  ggplot(aes(x = A, y = E, color = type)) +
  geom_point() +
  geom_line()

reprex package (v2.0.0)

于 2021-05-31 创建

试一试

library(dplyr)
library(ggplot2)

line_1_data <- data %>%
  select(A_w, E_w) %>%
  mutate(xend = lead(A_w), yend = lead(E_w)) %>%
  filter(!is.na(xend))

line_2_data <- data %>%
  select(A_e, E_e) %>%
  mutate(xend = lead(A_e), yend = lead(E_e)) %>%
  filter(!is.na(xend))

# multiple column for with different geom
ggplot(data = data) +
  # The blue line
  geom_point(aes(x = A_w, y = E_w), color = "blue") +
  geom_curve(data = line_1_data, aes(x = A_w, y = E_w, xend = xend,
    yend = yend), color = "blue",
    curvature = 0.02) +
  # The orange line
  geom_point(aes(x = A_e, y = E_e), color = "orange") +
  geom_curve(data = line_2_data,
    aes(x = A_e, y = E_e, xend = xend, yend = yend), color = "orange",
    curvature = -0.02) +
  # The red connection between two line
  geom_curve(data = tail(data, 1),
    aes(x = A_w, y = E_w, xend = A_e, yend = E_e), curvature = 0.1,
    color = "red") +
  # The black straight line between pair
  geom_curve(
    aes(x = A_w, y = E_w, xend = A_e, yend = E_e), curvature = 0,
    color = "black")

reprex package (v2.0.0)

于 2021-05-31 创建