Using ggplot to plot a map from a matrix
I wanted to create some maps by using ggplot2, but I am struggling to find the best way. Since I have a list ("models") with matrix, I wanted to use ggplot because then, it should be easier to create a multiplot (with facet_wrap..but just a thought). I will create an reproducible example for doing this:
#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:
#Now, as a second try to add a map of Europe, I used: 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(colours=cols) map <- map + facet_grid(model~Season) map <- map + theme(legend.position="none")
Now, I would have:
The problem with the second one is that I don't know how to change to background with white, and make the map more visible..is that possible?? The thing is, I wanted to plot 11 rows by 4 col. so when plotting all together it doesn't look very well...any suggestion?
Really appreciate any help.
Many thanks
链接地址: http://www.djcxy.com/p/30850.html上一篇: 在ggplot2中心绘图标题
下一篇: 使用ggplot从矩阵中绘制一张图