zhiwei li
zhiwei li

Reputation: 1711

How can I make a upside down map in a right positon in R

Context

I am using terra::rast() to read a nc file and plot it.

But the ploted map is upside down.

When I used raster::raster() to read the nc file and plot it. The map is right.

Question

How can I make the upside down map right when using terra::rast() to read the nc?

Reproducible code

I have uploaded pm2.5_year_2013.ncdata on my github. The data can be found at here

library(terra)
air = rast('pm2.5_year_2013.nc')
plot(air) # an upside down China map

air2 = raster::raster('pm2.5_year_2013.nc')
plot(air2) # an right China map

Upvotes: 0

Views: 471

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47411

It looks like this was fixed in the development version of "terra" (or the version of GDAL that it uses on windows)

install.packages('terra', repos='https://rspatial.r-universe.dev')

And now it works as expected.

library(terra)
#terra 1.6.30
r <- rast("pm2.5_year_2011.nc")
plot(r)

enter image description here

Upvotes: 2

SamR
SamR

Reputation: 20444

I am guessing this is a quirk of the data rather than than the terra package. I didn't try loading it in raster though - it does seem strange it would be the other way round in a different package.

In any case you can use terra::flip() to make it the right way around.

air2  <- terra::flip(air, direction="vertical")
plot(air2) 

enter image description here

Upvotes: 2

Related Questions