簡體   English   中英

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

[英]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

代表 package (v2.0.0) 於 2021 年 5 月 31 日創建

我正在嘗試 plot 這個數據與所有 A 值作為 X 和 Es 作為 Y。我怎樣才能把這兩個列繪制在 ggplot2 上,或 b)重新排列這個 Z6A8064B5DF47945557DZ5 列合並到 A 列 EC 550最終的 dataframe 只有兩列,行數是圖片的 2 倍?

感謝您的幫助,我是初學者(顯然)

為清晰而編輯:A_e 和 E_e 值保持成對很重要,類似於 A_w 和 E_w 值保持成對的方式。 最終結果 plot 應該類似於此圖像的橙色和藍色線,但我在學習 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

第1部分

和第二個 plot

# 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

情節第 2 部分

但我的最終目標是將它們都放在同一個 plot 上,就像上面的 Excel 圖表(橙色 + 藍色圖表)一樣。

你可以試試這個

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

代表 package (v2.0.0) 於 2021 年 5 月 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")

代表 package (v2.0.0) 於 2021 年 5 月 31 日創建

“手工”操作:

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

Output:

> 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

對於 plot: ggplot(df2, aes(x=A, y=E)) + geom_point()

Output:

在此處輸入圖像描述

有一些方法可以做到這一點,而不必通過列出它們的名稱來連接列 - 使用tidyr package - 但我認為這個解決方案對於初學者來說更容易理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM