为什么在添加ggplots列表时出现美学错误?

时间:2019-02-12 05:16:17

标签: r list ggplot2 aes

我对R还是很陌生,可能做了一些无关的编码,但是下面是一些模拟代码,用于说明我要对更大的数据集进行的处理。

# generate an n-length data frame with shown headers
n <- 1000
site <- sample(c("s1", "s2", "s3", "s4", "s5"), n, TRUE)
num1 <- sample(1:50, n, TRUE)
num2 <- sample(seq(1, 100, .01), n, TRUE)
date <- sample(seq(as.Date("2000-01-01"), as.Date("2005-12-31"), by="day"), n, TRUE)
year <- format(date, "%Y")
df1 <- data.frame(site, num1, num2, date, year, stringsAsFactors = FALSE)

# create empty vector
rows_num2max <- vector()

# find locations of the first occurrence of maximum 'num2' for each 'year'
# at each 'site' and append to vector
for (i in 1:length(unique(df1$site))) {
  df2 <- df1[df1$site == unique(df1$site)[i], ]
  for (j in 1:length(unique(df2$year))) {
    num2max <- max(unique(df1[which((df1$site == unique(df1$site)[i]) &
                                      (df1$year == unique(df2$year)[j])), 'num2']))
    row_num2max <- as.vector(min(which((df1$num2 == num2max) &
                                         (df1$site == unique(df1$site)[i]) &
                                         (df1$year == unique(df2$year)[j]))))
    rows_num2max <- append(rows_num2max, row_num2max)
  }
}

# get ggplot2
library(ggplot2)

# create empty list
events_plots <- list()

for (i in 1:length(rows_num2max)) {

  # subset data for a single set of plots
  # (50 days before and after maximum 'num2' occurrence)
  event <- df1[which((df1$date >= df1[rows_num2max[i], 'date'] - 50) &
                       (df1$date <= df1[rows_num2max[i], 'date'] + 50) &
                       (df1$site == df1[rows_num2max[i], 'site'])), ]

  # create a list of 3 plots with parameters as shown
  event_plots <- list(ggplot(event, aes(x = event[ , 'date'], y = event[ , 'num2'])) +
                        geom_point(),
                      ggplot(event, aes(x = event[ , 'date'], y = event[ , 'num1'])) +
                        geom_point(),
                      ggplot(event, aes(x = event[ , 'num1'], y = event[ , 'num2'],
                                        colour = event[ , 'date'])) +
                        geom_path(size = 1.1))

  # append the list of plots to the existing list
  events_plots[[length(events_plots)+1]] <- event_plots
}  

但是,当查看events_plots的每个元素时,出现以下错误:Error: Aesthetics must be either length 1 or the same as the data (##): x, y。关于减少和/或修复代码的任何提示?

编辑: 我已将每个请求中的sessionInfo()包括在内。

> sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] ggplot2_3.1.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0       withr_2.1.2      assertthat_0.2.0
 [4] crayon_1.3.4     dplyr_0.7.8      R6_2.3.0        
 [7] grid_3.5.2       plyr_1.8.4       gtable_0.2.0    
[10] magrittr_1.5     scales_1.0.0     pillar_1.3.1    
[13] rlang_0.3.1      lazyeval_0.2.1   rstudioapi_0.9.0
[16] bindrcpp_0.2.2   tools_3.5.2      glue_1.3.0      
[19] purrr_0.3.0      munsell_0.5.0    yaml_2.2.0      
[22] compiler_3.5.2   pkgconfig_2.0.2  colorspace_1.4-0
[25] tidyselect_0.2.5 bindr_0.1.1      tibble_2.0.1

0 个答案:

没有答案
相关问题