2

I want to plot USA raster using an Albers coordination,codes as follows:

#both shp_f and ras_f are WGS84 coordination
shp_f <- 'USA.shp'
ras_f <- 'USA.tif'
Albers <- crs('+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 
       +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs' )

shp <- readOGR(shp_f)
#convert raster cells as dataframe
ras_df <- ras_f %>% raster %>% rasterToPoints %>% as.data.frame
colnames(ras_df) <- c('x','y','val')

bm <- ggplot()+
  geom_tile(data=ras_df,aes(x=x,y=y,fill=val))+
  geom_polygon(data=shp,aes(x=long,y=lat,group=group),colour='grey',
           fill=NA,linetype='solid',size=0.1)+
  # convert plot coordination 
  ggalt::coord_proj(Albers) 

This is a common Albers projection map with arc-shaped latitude lines, and the xy labels are lon/lat degree. I want plot maps like it ,but it takes too much time to plot because convert each cell coordiantion in ggplot is very slow especially when the high raster resolution. So I consider that change the raster projection first ,then plot it. hence the second section codes as follows:

enter image description here

#convert shape polygon coordination  from WGS84 to Albers
shp_albers <- spTransform(shp, Albers)
ras_df <- ras_f %>% raster 
       #convert the raster coordination from WGS84 to Albers
        %>% projectRaster(., res=50000,crs=Albers) 
        %>% rasterToPoints %>% as.data.frame
colnames(ras_df) <- c('x','y','val')

bm <- ggplot()+
  geom_tile(data=ras_df,aes(x=x,y=y,fill=val))+
  geom_polygon(data=shp_albers,aes(x=long,y=lat,group=group),colour='grey',
           fill=NA,linetype='solid',size=0.1)

enter image description here

But this is a Cartesian graphic that xy is orthogonal (not a albers shape map). So the question is that: In general ,how to plot a proper shape map with it's projeciton (not a simple orthogonal graphic)? Meanwhile how to change the xy labels to lon/lat degree and plot arc-shape lon/lat lines (not use orthogonal lines)? The first figure is expected,but it plot too slow. The raster and shp file is here

Cobin
  • 888
  • 13
  • 25
  • `+ coord_equal()` – hrbrmstr Nov 29 '18 at 15:23
  • Hi, I'm unable to access your files without creating an account on the cloud storage site & downloading from there (which I doubt many here would be willing to do). Are you able to illustrate the issue with a commonly available raster / shapefile source? – Z.Lin Nov 30 '18 at 08:34
  • @Z.Lin I have refreshed file link. It would be downloaded. – Cobin Nov 30 '18 at 13:05
  • As before, I am not inclined to create an account on a cloud storage site that I don't otherwise use, for the sole purpose of downloading unverified files in order to answer SO questions. Do consider making your problem reproducible *within the question itself*. Check out [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [this](https://stackoverflow.com/help/mcve) if you haven't seen them before. – Z.Lin Dec 03 '18 at 05:40

0 Answers0