0

我想通过循环 ggplot 创建几个图。我创建了一个小 df ( plothelp_df ) 用于在循环期间派生绘图对象名称和变量名称。但是在尝试从plothelp_df派生名称时,我正在努力动态命名绘图对象。

这里的代码:

### Creating df which loop is based on
pltnames_scat <- c("plt_test2_blu","plt_test2_grn", "plt_test2_red", "plt_test2_re1", "plt_test2_re2", "plt_test2_re3", "plt_test2_bnr", "plt_test2_nir", "plt_test2_sw1", "plt_test2_sw2")
band_vnames <- c("BLUE", "GREEN", "RED", "REDEDGE1", "REDEDGE2", "REDEDGE3", "BROADNIR", "NIR", "SWIR1", "SWIR2")
band_appndx <- c("blu","grn", "red", "re1", "re2", "re3", "bnr", "nir", "sw1", "sw2")

plothelp_df <- data.frame(cbind(pltnames_scat, band_vnames, band_appndx))

### Principally I'd like to run a loop, but I am struggling with naming the plots dynamically
### Therefore the loop lines as comments and the indices in the plot section are set to 1 
# for (o in 1:nrow(plothelp_df)){
  noquote(plothelp_df$pltnames_scat[1]) <- ggplot(loopsubset_created_S02, aes_string(x = plothelp_df$band_vnames[1]  , y = "PdKeyT")) + 
  geom_point() +
  geom_point(mapping = aes(color = Class_Derived))
# }
plt_test2_blu

unquote(plothelp_df$pltnames_scat[1])应将名称plt_test2_blu分配给绘图,但以: 结尾
Error in noquote(plothelp_df$pltnames_scat[1]) <- ggplot( loopsubset_created_S02,:could not find function "noquote<-"
我也试过as.name(而不是unquote)。但是同样的错误。
如果我手动插入 index-1 绘图对象名称(= plt_test2_blu),绘图对象将按预期创建。因此,该错误似乎至少不是由其余代码引起的。

plt_test2_blu <- ggplot(loopsubset_created_S02, aes_string(x = plothelp_df$band_vnames[1]  , y = "PdKeyT")) + 
  geom_point() +
  geom_point(mapping = aes(color = Class_Derived))


不知何故,R 似乎不接受行首的函数。但我不知道如何解决这个问题。我对 R 很陌生。
有人可以告诉我正确的方法来动态命名地块吗?
(最后你会发现一个dput-df loopsubset_created_S02

这里的数据:

> dput(loopsubset_created_S02)
structure(list(Site_ID = c("A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A"), Spot_Nr = c("1", "1", "1", "1", 
"1", "1", "1", "1", "1", "1", "1", "1", "1"), Transkt_Nr = c("2", 
"2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2"), 
    Point_Nr = c("13", "13", "13", "13", "13", "13", "13", "13", 
    "13", "13", "13", "13", "13"), nobs = c(3L, 22L, 22L, 22L, 
    22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 2L), rank = c(3L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 15L), Tile = c("1008", 
    "1008", "1008", "1008", "1008", "1008", "1008", "1008", "1008", 
    "1008", "1008", "1008", "1008"), Date = c(20191023L, 20190506L, 
    20190603L, 20190625L, 20190630L, 20190710L, 20190725L, 20190809L, 
    20190817L, 20190903L, 20190913L, 20190921L, 20190524L), id = c("31", 
    "31", "31", "31", "31", "31", "31", "31", "31", "31", "31", 
    "31", "31"), Point_ID = c("1031", "1031", "1031", "1031", 
    "1031", "1031", "1031", "1031", "1031", "1031", "1031", "1031", 
    "1031"), Site_Nr = c("1", "1", "1", "1", "1", "1", "1", "1", 
    "1", "1", "1", "1", "1"), Point_x = c(356040.781, 356040.781, 
    356040.781, 356040.781, 356040.781, 356040.781, 356040.781, 
    356040.781, 356040.781, 356040.781, 356040.781, 356040.781, 
    356040.781), Point_y = c(5132780.701, 5132780.701, 5132780.701, 
    5132780.701, 5132780.701, 5132780.701, 5132780.701, 5132780.701, 
    5132780.701, 5132780.701, 5132780.701, 5132780.701, 5132780.701
    ), Classification = c(7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 
    2), Class_Derived = c("WW", "WF", "WF", "WF", "WF", "WF", 
    "WF", "WF", "WF", "WF", "WF", "WF", "NW"), BLUE = c(963, 
    2422, 2524, 1798, 1830, 1886, 1548, 1775, 2055, 1613, 1566, 
    1650, 2542), GREEN = c(1166, 2528, 2717, 2084, 2073, 2058, 
    1787, 1962, 2323, 1858, 1810, 1880, 2736), RED = c(1034, 
    2355, 2477, 2064, 2166, 2161, 1756, 2108, 2348, 1840, 1765, 
    1958, 2663), REDEDGE1 = c(1170, 2199, 2427, 2052, 2317, 2365, 
    1774, 2154, 2213, 1702, 1723, 2096, 2728), REDEDGE2 = c(917, 
    1670, 2077, 1880, 2087, 2163, 1879, 1972, 1986, 1409, 1435, 
    1943, 2450), REDEDGE3 = c(948, 1637, 2113, 1975, 2179, 2192, 
    1965, 2008, 2007, 1461, 1516, 1964, 2530), BROADNIR = c(826, 
    1727, 1990, 2047, 2048, 1983, 2203, 2004, 2133, 1574, 1554, 
    1823, 2303), NIR = c(918, 1590, 2036, 1910, 2172, 2098, 2029, 
    1987, 2008, 1437, 1486, 1899, 2451), SWIR1 = c(922, 1423, 
    2093, 1808, 2266, 2258, 1759, 2027, 2014, 1456, 1432, 1940, 
    2326), SWIR2 = c(709, 1080, 1468, 1362, 1565, 1600, 1295, 
    1416, 1523, 1104, 1077, 1395, 1648), Quality.assurance.information = c(8192, 
    8208, 8192, 8256, 8192, 8192, 8256, 8192, 8192, 8192, 8192, 
    8192, 8192), Q00_VAL = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0), Q01_CS1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    Q02_CS2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q03_CSH = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q04_SNO = c(0, 1, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q05_WAT = c(0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), Q06_AR1 = c(0, 0, 0, 1, 0, 0, 1, 
    0, 0, 0, 0, 0, 0), Q07_AR2 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), Q08_SBZ = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0), Q09_SAT = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    Q10_ZEN = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q11_IL1 = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q12_IL2 = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Q13_SLO = c(1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1), Q14_VAP = c(0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0), Q15_WDC = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), PdMax = c(5L, 57L, 44L, 18L, 13L, 16L, 13L, 
    9L, 7L, 11L, 9L, 2L, 47L), PdMin = c(0L, 47L, 37L, 14L, 9L, 
    9L, 9L, 5L, 3L, 2L, 5L, -3L, 36L), PdKeyT = c(3L, 53L, 43L, 
    15L, 13L, 13L, 10L, 7L, 6L, 6L, 9L, 2L, 40L)), row.names = c(50L, 
66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 81L), class = "data.frame")
4

1 回答 1

1

我建议不要将您的图存储为单个对象,而是将它们存储在(命名的)列表中,这通常是推荐的方法。此外,我建议通过例如lapply而不是使用for循环来创建您的图。最后,如果您想通过字符串访问列,aes_string您还可以使用.data代词 from 。rlang

library(ggplot2)

pltnames_scat <- c("plt_test2_blu","plt_test2_grn", "plt_test2_red", "plt_test2_re1", "plt_test2_re2", "plt_test2_re3", "plt_test2_bnr", "plt_test2_nir", "plt_test2_sw1", "plt_test2_sw2")
band_vnames <- c("BLUE", "GREEN", "RED", "REDEDGE1", "REDEDGE2", "REDEDGE3", "BROADNIR", "NIR", "SWIR1", "SWIR2")

# Set the names for the vector to loop over. 
# This will automatically return a named list with the plot names
names(band_vnames) <- pltnames_scat

plot_list <- lapply(band_vnames, function(vname) {
  ggplot(loopsubset_created_S02, aes(x = .data[[vname]], y = PdKeyT)) + 
    geom_point() +
    geom_point(mapping = aes(color = Class_Derived))
})

plot_list$plt_test2_blu

在此处输入图像描述

于 2021-11-16T17:46:26.620 回答