如何在R中绘制地理参考数据集?

时间:2015-06-24 05:12:42

标签: r ggplot2 geospatial spatial

我想要在美国地图上绘制这些网格化数据: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)

但是我得到一个倒置的情节。你能帮忙吗?

enter image description here

我之前在这里得到了类似数据集的答案:How to convert point data collected at grid interval to a georeferenced dataset in r?

1 个答案:

答案 0 :(得分:0)

与您之前提到的上一个问题中的数据集相比,此csv中的纬度值将被还原。您所要做的就是反转这个新数据集中的行号:

在你的行之后:

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

使用以下方式反转行号:

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

应该照顾它。

相关问题