【问题标题】:Multiple plots inside if condition Rif 条件 R 内的多个图
【发布时间】:2019-07-01 11:25:02
【问题描述】:

我正在使用if 条件来处理一些数据并创建三个不同的图,它们显示在Viewer

if (S2_input){
      S2_images<-stack(S2_rsp)
      S2_images
      cubeView(S2_images)

      # Plot True/False color
      viewRGB(S2_images, 3,2,1, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik"))
      viewRGB(S2_images, 4,3,2, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik"))
}

S2_images 在哪里:

> S2_images
class       : RasterStack 
dimensions  : 660, 1074, 708840, 30  (nrow, ncol, ncell, nlayers)
resolution  : 10, 10  (x, y)
extent      : 219800, 230540, 4097480, 4104080  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=30 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
names       : L2A_T30ST//51_B02_10m, L2A_T30ST//51_B03_10m, L2A_T30ST//51_B04_10m, L2A_T30ST//51_B08_10m, L2A_T30ST//51_B11_20m, L2A_T30ST//51_B12_20m, L2A_T30ST//21_B02_10m, L2A_T30ST//21_B03_10m, L2A_T30ST//21_B04_10m, L2A_T30ST//21_B08_10m, L2A_T30ST//21_B11_20m, L2A_T30ST//21_B12_20m, L2A_T30ST//51_B02_10m, L2A_T30ST//51_B03_10m, L2A_T30ST//51_B04_10m, ... 
min values  :                     1,                   127,                     6,                     1,                    88,                    86,                     1,                     1,                     1,                     1,                     1,                     1,                    50,                   198,                     7, ... 
max values  :                  8702,                  9090,                  7589,                  7322,                  5379,                  5474,                  8743,                  9298,                  7585,                  8530,                  5712,                  5905,                  8048,                  7692,                  7187, ... 

问题来了,当我运行完整的if 语句时,查看器中只显示最终的viewRGB。知道如何在条件内创建所有这些吗?

【问题讨论】:

    标签: r loops if-statement plot view


    【解决方案1】:

    正如您在此处看到的Printing 'Hello world' n number of times in R,R 仅显示循环内的最后一条指令,因此解决您的问题的方法是将您的地图包装在 print() 中,所以:

    if (S2_input){
          S2_images<-stack(S2_rsp)
          S2_images
          cubeView(S2_images)
    
          # Plot True/False color
          print(viewRGB(S2_images, 3,2,1, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik")))
          viewRGB(S2_images, 4,3,2, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik"))
    }
    

    if (S2_input){
          S2_images<-stack(S2_rsp)
          S2_images
          cubeView(S2_images)
    
          # Plot True/False color
          print(viewRGB(S2_images, 3,2,1, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik")))
          print(viewRGB(S2_images, 4,3,2, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik")))
    }
    

    或者你也可以构建一个地图向量:

    if (S2_input){
          S2_images<-stack(S2_rsp)
          S2_images
          cubeView(S2_images)
    
          # Plot True/False color
          v <-c(viewRGB(S2_images, 3,2,1, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik")),viewRGB(S2_images, 4,3,2, map.types=c("Esri.WorldTopoMap", "Esri.WorldImagery", "OpenStreetMap.Mapnik")))
          v
    }
    

    【讨论】:

    • 太好了,必须检查 R 基础知识 :)
    猜你喜欢
    • 1970-01-01
    • 2014-09-06
    • 2015-04-21
    • 2017-06-25
    • 2016-01-20
    • 2015-02-07
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    相关资源
    最近更新 更多