使用ggplot从矩阵中绘制一张图

我想通过使用ggplot2创建一些地图,但我正在努力寻找最佳方式。 由于我有一个带有矩阵的列表(“模型”),因此我想使用ggplot,因此创建一个多槽(与facet_wrap ..但只是一个想法)应该更容易。 我将为此创建一个可重复的示例:

#European corrdinates
lon <- rep(loni,38)  #longitude values -13W-34E
lat <- rep(lati, each = 48)  #latitude values 34N-70N
#Matrix of values
mod <- matrix( rnorm(48*38,mean=0,sd=1), 48, 38)
#Create a list of matrix (as my real data)
dat.mod <- rep(list(mod), 4)
names(dat.mod) <- c("DJF","MAM","JJA","SON")

#Order data
md <- melt(dat.mod)
md$lon <- lon
md$lat <- lat
md$group <- "Model1"
names(md) <- c("X1","X2","SI","Season","lon","lat","model")
#
#First try: 
#Problem here: I don't know how to add the map
  brks <- seq(0, 2.2, by=0.2)
 cols <- colorRampPalette(c("skyblue1", "lightcyan2", "lightyellow",   "yellow", "orange", "red3"))(length(brks)-1)
 m <- ggplot(data = md, aes(x = lon, y = lat, fill = SI)) + 
 geom_raster() +
 scale_fill_gradientn(colours = cols, na.value = NA)+
 facet_wrap(~ model~ Season, nrow =11,ncol=4)+
 theme(strip.background = element_blank(),
 strip.text.x = element_blank())
 #I would get this image:

在这里输入图像描述

#现在,第二次尝试添加欧洲地图,我使用:map < - ggplot()map < - map + coord_fixed()map < - map + geom_polygon(data = map_data(map =“world”),aes (x = long,y = lat,group = group),fill = NA,color =“black”,size = 0.01)map < - map + coord_cartesian(xlim = range(md $ lon),ylim = range(md $ lat))map < - map + geom_tile(data = md,aes(x = lon,y = lat,fill = SI),alpha = I(0.7))map < - map + scale_fill_gradientn(colors = cols)map <地图+ facet_grid(模型〜季节)地图< - 地图+主题(legend.position =“无”)

现在,我会拥有: 在这里输入图像描述

第二个问题是,我不知道如何改变为白色的背景,并使地图更明显..这可能吗? 事情是,我想绘制4行4列的11行。 所以当密谋一起时,它看起来不太好......有什么建议吗?

真的很感谢任何帮助。

非常感谢

链接地址: http://www.djcxy.com/p/30849.html

上一篇: Using ggplot to plot a map from a matrix

下一篇: ggplot2 multiple plots keeps old plots