R和ggplot(或其他) - 我如何在国家边界内绘制瓷砖图?

时间:2013-10-29 07:32:52

标签: r ggplot2 gis

我想用网格数据(1度网格)制作中国地图,例如显示空气温度。我希望用颜色显示差异 - 但仅限于国家边界 - 或者包括国家边界以外的一个小乐队。我还希望在该区域拥有平滑的渐变颜色。 我尝试了ggplot(参见示例)并且还看了一些空间包但是卡住了。我来自SAS,这就像是自动的 - 所以我迷失了。 R版本3.0.2,Windows,64位

帮助。 奥莱

    #Plotproblem_1 - How to make gradient plot inside country bounds?
#grid - Ugly but works for now - real data set is 66 x 40 - this is
# 10 x 12 and faked data
library(maps);library("mapdata");library("ggplot2")

x<-(rep(1:12,10));y<-c(rep(1,12),rep(2,12),rep(3,12),rep(4,12),rep(5,12),
                   rep(6,12),rep(7,12),rep(8,12),rep(9,12),rep(10,12))
var<-rnorm(120)+10; lon<-64+6*x; lat<-11+4*y; #   71-137, 14-51
# ll china 70.5E, 15.5N, ur china  135.5E, 54.5N
df<-data.frame(lon,lat,var)
#plot(df)

#Define nice colour scheme
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", 
                             "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))

china<-map("worldHires","china",fill=FALSE, col="black") 

windows(8,5) #Colored dots -good looking
china_reg2 <- fortify(map("china", ylim =c(17,55), fill=FALSE,plot=FALSE)) 
#balloon plot just for check
p2 <- ggplot(china_reg2) 
p2 + geom_path(aes(x=long, y=lat, group=group), color="black") + 
geom_point(data=df, aes(x=lon, y=lat, colour = var),size=5,shape=15) +
scale_colour_gradientn(colours = jet.colors(7)) +
labs(title = "Fake China data",
   plot.title = element_text(face="bold", size=14)) 

windows(8,5) #Tile plot - how to only color inside China border (or with some nice overlap incl. Hainan & Taiwan)??
p2 <- ggplot(china_reg2) 
p2 + geom_tile(data=df, aes(x=lon, y=lat, fill=var)) +
geom_path(aes(x=long, y=lat, group=group), color="black", size=1.2) + 
scale_fill_gradientn( colours=c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))+
labs(title = "Fake data China", plot.title = element_text(face="bold", size=14)) 

0 个答案:

没有答案