如何在 R 中绘制具有 excel(在不同工作表中)的多个 X、Y(向量)的散点图

How to plot a scatter plot with multiple X, Y's (vectors) from an excel (in different sheets) in R

我在 excel 文件中有一个大型数据集,其中 excel 文件包含多个作品 sheet。在每个作品sheet中,我有大小为 m*n 的 xi 和 Yi 向量(列向量,其中 i = 1,2,3....n),其中 m 可以是 xi 的任意行数和义。

我在 excel 文件中的示例数据集如下所示:

Excel file name: mydata; Sheet Name: data1

T324X_w T324Y_w T332X_w T332Y_w T36X_w  T36Y_w  T41X_w  T41Y_w  
7.67562 57.6316 7.41441 57.713  7.3051  58.349  7.6438  57.218      
12.0346 60.1571 10.3314 53.887  11.250  53.327  12.164  51.912      
17.2237 45.5066 13.9237 78.237  16.102  46.838  30.048  45.656  
21.8646 55.3352 35.3032 19.981  20.565  41.655  22.441  39.498      
15.9111 34.9817 36.5099 41.203  5.404   18.42   27.487  33.855  
31.6832 40.842  24.185  47.317  8.361   31.123  23.046  48.36   
15.7659 25.880  19.629  43.792  4.659   26.810  37.157  24.490  
10.5192 21.909  21.400  45.107  9.081   22.799  42.152  28.351                      
15.724  35.202  14.579  27.205  44.006  18.97   47.415  16.778                  
11.352  14.6283 27.956  24.449  49.572  55.464  53.3125 13.0234                 
25.740  39.320  21.759  21.609  55.448  11.915  58.992  29.9650                 
                25.752  18.874  60.713  9.641   66.19   6.859                       
                29.63   84.745  67.34   6.596   73.18   3.618                   
                53.28   14.26                   81.1    0.6252                      


Excel file name: mydata; Sheet Name: data2

T325X_w T325Y_w T333X_w T333Y_w 
7.67562 57.6316 7.41441 7.713   
12.346  12.1571 10.3314 3.89    
27.37   15.5066 13.237  29.27   
21.46   10.3352 17.3032 24.1        
26.111  14.9817 20.099  21.2    
31.832  19.842  24.185  37.37       
35.659  25.880  27.29   23.2    
20.192  21.909  11.00   30.7        
45.24   18.202  34.579  27.205  
31.52   14.6283 27.56   24.9    
36.740  12.320  41.59   21.09   
                45.752  18.4                            
                49.63   56.45                   
                53.28   14.26                                           


 Excel file name: mydata; Sheet Name: data3

T325X_w T324Y_w T332X_w T332Y_w T36X_w  T36Y_w  T41X_w  T41Y_w  
7.2 16  7.1441  47.13   7.051   48.349  7.6438  37.218  22.145
12.9346 2.1571  10.14   53.887  11.250  53.327  12.164  31.12   
17.2237 5.0     13.237  49.237  16.102  46.38   17.048  45.656  
21.46   0.52    37.3032 24.1    20.565  41.655  42.441  39.498  
26.11   4.17    40.9    21.03   15.404  3.42    87.487  33.855      
31.2    9.42    44.185  27.317  10.361  1.123   73.046  28.36       
35.59   5.0     27.629  33.792  24.659  6.810   37.157  24.490      
40.2    21.09   31.400  30.107  9.081   2.799   42.152  20.351                  
45.24   18.02   24.579  37.205  44.006  8.97    47.415  16.778                  
51.2    14.3    37.759  44.449  29.572  15.464  63.3    13.0                    
55.740  12.320  41.759  41.09   25.448  1.15    38.992  9.0                     
                45.759  68.874  20.713  9.641   36.19   6.5                     
                39.63   66.45   67.34   6.596   43.18   3.1                 
                53.28   14.26                   81.1    0.252                       

我想在单个散点图上绘制所有 sheet 中的所有 Xi 与 Yi 的对比

注意:基于sheet的散点图:data1、data2、data3应该是三种不同的颜色。

显示在 excel 中绘制的预期散点图

此处显示的数据与图表对应的数据可能有所不同

我正在尝试的代码如下

library(readxl)    
read_excel_allsheets <- function(filename, tibble = FALSE) {
  sheets <- readxl::excel_sheets(filename)
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
  if(!tibble) x <- lapply(x, as.data.frame)
  names(x) <- sheets
  x
}
mysheets <- read_excel_allsheets("mydata.xlsx")

plot(mydata$data1$T324X_w, mydata$data1$T324Y_w, type = "l", col = "red", xlab = "X_axis", ylab = "Y_Axis", main = "Plots")
lines(mydata$data1$T332X_w, mydata$data1$T332Y_w, type = "l", col = "red")
lines(mydata$data2$T333X_w, mydata$data2$T333Y_w, type = "l", col = "blue")
legend("topright", c("T324d1", "T332d1", "T333d2"), 
    text.col=c("red", "yellow", "green"),pch = c(1,2), lty = c(1,2))

注意:图例名称应该是例如 "T324" 是向量的名称,"d1" 是 sheet 名称作为 T324d1 即指定向量的名称和 sheet

通过 xi 与 yi 向量绘制个体是一个乏味且冗长的过程

我正在寻找一个代码,用于在单个图中绘制来自所有 sheet 的 Xi 和 Yi 的所有列向量

提前致谢

我试图解决使用 for 循环的问题,它对我有用......但唯一的问题是图例

##################### code to read all worksheets from an excel file ###############
library(readxl)    

read_excel_allsheets <- function(filename, tibble = FALSE) {
sheets <- readxl::excel_sheets(filename)
x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
if(!tibble) x <- lapply(x, as.data.frame)
names(x) <- sheets
x
}
########################################################
mysheets <- read_excel_allsheets("mydata.xlsx")  ## reading excel file
mysheets <- mysheets[-1] # removing the unwanted dataframe

for (i in 1: length(mysheets)) {
     a <- data.frame(mysheets[i])

     colors <- c("red", "blue", "green", "yellow", "black")

     for (j in seq(from = 1, to = length(a), by = 2)){

         b <- plot(a[, j], a[, j+1], type = "l", xlab = "X_Axis", ylab = "Y_Axis", xlim = c(0,100), ylim = c(-10,70), col = colors[i], main = "Trajectories")

         par(new = T)

      }
}

它对我有用...... 然而,我并没有像预期的那样得到情节上的传说。 有没有其他方法可以绘制散点图.....我们可以在哪里获得缩放 in/out 和其他特征以及图例?