简体   繁体   中英

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

Created on 2021-05-31 by the reprex package (v2.0.0)

I am trying to plot this data with all A values as X and Es as Y. How can I put either a) both of these columns plotted on a ggplot2, or b) rearrange this dataframe to combine the A columns and E columns into a final dataframe with only two columns with 2x as many rows as pictured?

Thanks for any help, I am a beginner (obviously)

Edit for Clarity: It's important that the A_e & E_e values remain as pairs, similar to how the A_w and E_w values remain as pairs. The end result plot should resemble the ORANGE and BLUE lines of this image, but I am trying to replicate this while learning R.剧情决赛

Currently I am capable of plotting each separately when dividing into two dataframes of 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部分

and the second 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 部分

But my end goal is to have them both on the same plot, like in the Excel graph (orange + blue graph) above.

You may try from this

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

Created on 2021-05-31 by the reprex package (v2.0.0)

Here is a try

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

Created on 2021-05-31 by the reprex package (v2.0.0)

Doing it "by hand":

#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

And for the plot: ggplot(df2, aes(x=A, y=E)) + geom_point()

Output:

在此处输入图像描述

There are ways to do this without having to joint the columns by listing their names - with the tidyr package - but i think that this solution is easier to understand from a beginners pov.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM