如何使用ggplot2按文件名创建单独的图?

时间:2019-07-14 15:03:11

标签: r dataframe ggplot2 graph subset

我正在尝试一个数据集,其中使用不同的文件名(它们在“名称”列中出现多次)。例如,我在所有行中多次出现“ ML_Set_B01_0001”:

Name            Mode.Y.  PA.Y.  paMeasurement   avgVal

ML_Set_B01_0001 1       -28     1               -29.74
ML_Set_B01_0002 1       -42     1               -45.00
ML_Set_B01_0003 1       -27     1               -29.74
ML_Set_B01_0004 2        6      1                 2.34
ML_Set_B01_0005 3       -39     1               -18.43
ML_Set_B01_0006 5        27     1                63.43
ML_Set_B01_0007 4        35     1                 7.59
ML_Set_B01_0008 2        -9     1                -5.57
ML_Set_B01_0009 2       -20     1                -6.91
ML_Set_B01_0010 1       -39     1                -38.66
....
ML_Set_B01_0001 ..    .....    ....     .....
ML_Set_B01_0002 ..    .....    ....     .....
....

我正在尝试绘制每个文件名的数据(avgVal与PA.Y)。到目前为止,我有以下代码:

我试图编写一个按文件名分隔数据的函数,以便按文件名绘制数据:

#Read CSV file
pa1 <- read.csv("pa_result_1.csv")
```
```{r}
#Restructure data file
pa1_clean <- pa1 %>%
  gather(paMeasurement, avgVal, num_range('X', 1:217)) %>%
  mutate(paMeasurement = readr::parse_number(paMeasurement))

pa1_clean
file.graph <- function(df, na.rm = TRUE, ...) {
  #Create a list of files to loop over. Produces 360 items in the list, which is how many files we have.
  file_list <- unique(df$Name)

  #Create a loop to produce ggplot2 graphs
  for (i in seq_along(file_list)) {
    #Create a plot for each file in the pa1_clean data frame
    pa1_plot <- 
      ggplot(subset(df, df$Name == file_list[i]), 
             aes(x = df$avgVal., y = df$PA.Y., group = df$Name, colour = df$Mode.Y.)) +
               geom_point() + facet_wrap( ~ df$Mode.Y., ncol = 2) + theme_pander() + 
      theme(legend.position = "none") +
      scale_y_continuous("Known Pitch Angles") + 
      scale_x_continuous("Pitch Angle Measurement for a Given Inner Radius of the Annulus") +
      ggtitle(paste(file_list[i], ' Dataset Results', sep = ''))
    print(pa1_plot)
  }
}

file.graph(pa1_clean)

因为我总共有360个文件,所以我试图使用for循环遍历每个文件名。

执行此代码后,出现错误:

Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 1L, 1L, 2L, : replacement has 78120 rows, data has 217

有人可以向我解释此错误吗?如何使用ggplot2按文件名生成图?

0 个答案:

没有答案