How can I plot a georeferenced dataset in R?

2019-08-10 21:25发布

问题:

I have this gridded data that I want to plot on a US map: https://www.dropbox.com/s/9khcjgtv8ipo2u5/windspeed.txt?dl=0

library(ggplot2)
library(RColorBrewer)
library(rgdal)
library(sp)
library(maps)
options(max.print=5.5E5) 

all_data = read.table("windspeed.txt",header = TRUE)

res=0.01 #spacing of row and col coords pre-specified
origin_lat_lon=c(24.55, -130) 
all_data$row=(all_data$row)*res+origin_lat_lon[1] 
all_data$col=(all_data$col)*res+origin_lat_lon[2]
coords = cbind(all_data$col, all_data$row)
spdf = SpatialPointsDataFrame(coords, data=all_data) #sp = SpatialPoints(coords)
proj4string(spdf) <- CRS("+init=epsg:4269") 

df=as.data.frame(spdf)
myPalette <- colorRampPalette(rev(brewer.pal(10, "Spectral")))
usamap <- map_data("state")
ggplot(data=df,aes(x=col,y=row,color=m)) + 
  geom_polygon( data=usamap, aes(x=long, y=lat,group=group),colour="black", fill="white" )+
  geom_point()+
  scale_colour_gradientn(name = "Wind",colours = myPalette(10), limits=c(0,1))+
  xlab('Longitude')+
  ylab('Latitude')+
  theme_bw()+
  theme(line = element_blank())+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#854440"))+
  ggsave("test.png",width=10, height=8,dpi=300)

But I am getting an inverted plot. Can you please help?

I previously got an answer for a similar dataset here: How to convert point data collected at grid interval to a georeferenced dataset in r?

回答1:

Your latitude values in this csv are reverted when compared to the dataset you previously had from the previous question you mentioned. All you have to do is to invert the row numbers in this new dataset:

Right after your line:

all_data = read.table("windspeed.txt",header = TRUE)

Invert the row numbers using:

max_row= max(all_data$row)
all_data$row=max_row-all_data$row 

That should take care of it.