本文介绍了彩色网格单元格在美国和加拿大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想在美国和加拿大彩色网格单元格。我的目标非常类似于这个问题: R Plot填充经度 - 纬度网格单元格在地图上然而,这个问题只涉及美国,我不知道如何添加加拿大。 我能画出美国和加拿大的地图可以通过修改此处找到的代码: https:// groups。 google.com/forum/#!topic/ggplot2/KAKhoE0GO4U 库(ggplot2)库(rgeos)库(maps)库(maptools) PolygonCoords< - 函数(多边形){多边形 coords.list< - lapply(seq_along(polygons)),function(i){#提取组,序列,区域,经度和纬度 coords< - 多边形[[i] ] @coords cbind(i,1:nrow(coords),多边形[[i]] @ area,coords)}) coords.df< - as.data.frame(do.call(rbind,coords.list))名称(coords.df)< - c(order ,seq,area,long,lat) return(coords.df)} ConvertWorldSimple< - function(mapdata, area = 0){ coords.list< - lapply(mapdata @ polygons,PolygonCoords) ncoords< - sapply(coords.list,nrow) coords.df < - do.call(rbind,coords.list) coords.df $ country< - rep(mapdata @ data $ NAME,ncoords) country.group< - factor(paste 。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $区域> = min.area,] 返回(coords.df)} 数据(wrld_simpl) world< - ConvertWorldSimple(wrld_simpl, min.area = 0.1) 世界< - 世界[%c('美国','加拿大')的世界$国家%,] na< - data.frame( country = c(United States,Canada), is.north.america = TRUE ) world< - merge(world,na,all.x = TRUE) world $ is.north.america [is.na(world $ is.north.america)] < - FALSE world< - world [order(world $ order,world $ seq),] ggplot(world,aes(long,lat,group = group)+ geom_polygon(aes(fill = is.north.america))+ geom_path(color =white,size = 0.1)+ scale_fill_manual(values = c +$,= - 4)* scale_y_continuous(,休息=( - 2:2)* 30)+ scale_x_continuous 45)+ coord_equal()+ theme_bw() 这里是代码为网格单元创建假属性数据,在这里找到: http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/ set.seed(1234) xlim = c(-110,-100) ylim = c(40,60) dat_grid = expand.grid(x = xlim [1]:xlim [2],y = ylim [1]:ylim [2]) dat_grid $ z = runif(nrow(dat_grid)) 头(dat_grid) 这里是 ggplot2 在早期Stack Overflow文章中使用的代码在Lower 48的地图上覆盖一个属性网格: library( $($) us_states< - map_data(state)(ggplot(aes(x = x,y = y,fill = z),data = dat_grid) + geom_tile())+ geom_polygon(data = us_states,aes(x = long,y = lat,group = group),color =black,fill =white,alpha = 0) 如何组合两个 ggplot 语句来覆盖虚假属性网格数据到美国和加拿大的地图上?感谢您的任何建议。解决方案这应该做的工作 library(ggplot2) library(maps) us = map_data(state)#或者如果你不想要州的边界#us = map_data(states,boundary = FALSE) ca = map_data(world,Canada) set.seed 1234) xlim = c(-110,-100) ylim = c(40,60) dat_grid = expand.grid(x = xlim [1]:xlim [2] y = ylim [1]:ylim [2]) dat_grid $ z = runif(nrow(dat_grid)) p = ggplot(aes(x = x,y = y,fill = z),data = dat_grid)p + geom_tile()+ geom_polygon(data = us,aes(x = long,y = lat,group = group),color =black,fill =white = 0)+ geom_polygon(data = ca,aes(x = long,y = lat,group = group),color =black,fill =white,alpha = 0) 如果您需要阿拉斯加: library(ggplot2)库(maps) m = map_data(world2,c(usa,Canada)) set .seed(1234) xlim = c(250,300) ylim = c(40,60) dat_grid = expand.grid(x = xlim [1]:xlim [2],y = ylim [1]:ylim [ 2]) dat_grid $ z = runif(nrow(dat_grid)) p = ggplot(dat_grid,aes(x = x,y = y))+ geom_tile(aes(fill = z )p + geom_polygon(data = m,aes(x = long,y = lat,group = group),color =black,fill =white,alpha = 0) I would like to color grid cells in the United States and Canada. My goal is very similar to this question: R Plot Filled Longitude-Latitude Grid Cells on Map However, that question only deals with the United States and I cannot figure out how to add Canada.I was able to draw a map of the U.S. and Canada by modifying code found here: https://groups.google.com/forum/#!topic/ggplot2/KAKhoE0GO4Ulibrary(ggplot2)library(rgeos)library(maps)library(maptools)PolygonCoords <- function(polygon) { polygons <- polygon@Polygons coords.list <- lapply(seq_along(polygons), function(i) { # Extract the group, sequence, area, longitude, and latitude. coords <- polygons[[i]]@coords cbind(i, 1:nrow(coords), polygons[[i]]@area, coords) }) coords.df <- as.data.frame(do.call(rbind, coords.list)) names(coords.df) <- c("order", "seq", "area", "long", "lat") return(coords.df)}ConvertWorldSimple <- function(mapdata, min.area = 0) { coords.list <- lapply(mapdata@polygons, PolygonCoords) ncoords <- sapply(coords.list, nrow) coords.df <- do.call(rbind, coords.list) coords.df$country <- rep(mapdata@data$NAME, ncoords) country.group <- factor(paste(coords.df$country, coords.df$order)) coords.df$group <- as.numeric(country.group) coords.df <- coords.df[coords.df$area >= min.area, ] return(coords.df)}data("wrld_simpl")world <- ConvertWorldSimple(wrld_simpl, min.area = 0.1)world <- world[world$country %in% c('United States', 'Canada'),]na <- data.frame( country = c("United States", "Canada"), is.north.america = TRUE)world <- merge(world, na, all.x = TRUE)world$is.north.america[is.na(world$is.north.america)] <- FALSEworld <- world[order(world$order, world$seq), ]ggplot(world, aes(long, lat, group = group)) + geom_polygon(aes(fill = is.north.america)) + geom_path(color = "white", size = 0.1) + scale_fill_manual(values = c("darkgray"), guide = "none") + scale_y_continuous("", breaks=(-2:2) * 30) + scale_x_continuous("", breaks=(-4:4) * 45) + coord_equal() + theme_bw()Here is code to create fake attribute data for the grid cells, found here: http://www.numbertheory.nl/2011/11/08/drawing-polar-centered-spatial-maps-using-ggplot2/set.seed(1234)xlim = c(-110,-100)ylim = c(40,60)dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])dat_grid$z = runif(nrow(dat_grid))head(dat_grid)Here is the ggplot2 code used in the earlier Stack Overflow post to overlay a grid of attributes on a map of the Lower 48:library(ggplot2)library(maps)us_states <- map_data("state")(ggplot(aes(x=x,y=y,fill=z),data=dat_grid) + geom_tile())+geom_polygon(data=us_states,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0)How can I combine the two ggplot statements to overlay the grid of fake attribute data onto the map of the U.S. and Canada? Thank you for any advice. 解决方案 This should do the joblibrary(ggplot2)library(maps)us = map_data("state")# or this if you don't want the states' boundary# us = map_data("states", boundary=FALSE)ca = map_data("world", "Canada")set.seed(1234)xlim = c(-110,-100)ylim = c(40,60)dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])dat_grid$z = runif(nrow(dat_grid))p = ggplot(aes(x=x,y=y,fill=z),data=dat_grid)p + geom_tile() + geom_polygon(data=us,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0) + geom_polygon(data=ca,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0)If you need Alaska:library(ggplot2)library(maps)m = map_data("world2", c("usa", "Canada"))set.seed(1234)xlim = c(250,300)ylim = c(40,60)dat_grid = expand.grid(x = xlim[1]:xlim[2], y = ylim[1]:ylim[2])dat_grid$z = runif(nrow(dat_grid))p = ggplot(dat_grid,aes(x=x,y=y)) + geom_tile(aes(fill=z))p + geom_polygon(data=m,aes(x=long, y=lat, group=group), colour="black", fill="white", alpha=0) 这篇关于彩色网格单元格在美国和加拿大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-14 14:43