0

I have the following libraries installed:

library(rgdal)
library(raster)
require(sf)
library(ggplot2)
library(raster)
library(rgeos)
library(tidyverse)
library(leaflet)
library(sp)
library(bnspatial)

The tif files that are used for plotting are imported via

#import tif-files
rlist=list.files(getwd(), pattern="tif$", full.names=FALSE)
for(i in rlist) { assign(unlist(strsplit(i, "[.]"))[1], raster(i)) }

> print(rlist)
 [1] "nightlight_3150.tif"   "nightlight_450.tif"    "population_1000.tif"  
 [4] "population_3000.tif"   "road_class_1_5000.tif" "road_class_2_100.tif" 
 [7] "road_class_2_25.tif"   "road_class_2_50.tif"   "road_class_3_300.tif" 
[10] "road_class_3_3000.tif" "trop_mean_filt.tif" 

I am trying to plot several of those tif files via a list tifs:

tifs = list(nightlight_3150, nightlight_450, population_1000, population_3000).

The list of tif files will be used for a for loop. When I print the first item of the list tifs I get the following output:

> print(tifs[1])
[[1]]
class      : RasterLayer 
dimensions : 2000, 2000, 4e+06  (nrow, ncol, ncell)
resolution : 3e-04, 3e-04  (x, y)
extent     : 4.5817, 5.1817, 52.0697, 52.6697  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : nightlight_3150.tif 
names      : nightlight_3150 
values     : 0, 97.12836  (min, max)

When I try to plot the first item in the list I get the error:

> plot(tifs[1])
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' is a list, but does not have components 'x' and 'y'

I tried to apply the proposed solution of another post ('x' is a list, but does not have components 'x' and 'y') to my code. However, doing so also results in an error:

> plot(tifs[1]$(4.5817, 5.1817), tifs[1]$(52.0697, 52.6697))
Error: unexpected '(' in "plot(tifs[1]$("

What is the correct way of plotting each tif file from a list?

Thanks

0 Answers0