【问题标题】:world map - map halves of countries to different colors世界地图 - 将国家的一半映射为不同的颜色
【发布时间】:2012-11-04 14:26:08
【问题描述】:

我在这里使用示例进行讨论: ggplot map with l

library(rgdal)
library(ggplot2)
library(maptools)

# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'

gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")

n <- length(unique(world.ggmap$id))
df <- data.frame(id = unique(world.ggmap$id),
                 growth = 4*runif(n),
                 category = factor(sample(1:5, n, replace=T)))

## noise
df[c(sample(1:100,40)),c("growth", "category")] <- NA


ggplot(df, aes(map_id = id)) +
     geom_map(aes(fill = growth, color = category), map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
     scale_fill_gradient(low = "red", high = "blue", guide = "colorbar")

给出以下结果:

我想将一个变量映射到一个国家的左侧“一半”,并将另一个变量映射到该国家的右侧“一半”。我将“一半”放在引号中,因为它没有明确定义(或者至少我没有明确定义它)。 Ian Fellows 的回答可能会有所帮助(这提供了一种获取质心的简单方法)。我希望有一些东西可以在示例中执行aes(left_half_color = growth, right_half_color = category)。如果不同的话,我也对上半部分和下半部分感兴趣。

如果可能,我还想将两半的各个质心映射到某个东西。

【问题讨论】:

  • 您可能需要考虑将两张地图并排放置。可能比这种国家分裂更直观地看待和解释。
  • @Marcinthebox 感谢您的建议。

标签: r map ggplot2


【解决方案1】:

这是一个没有 ggplot 的解决方案,而是依赖于 plot 函数。除了 OP 中的代码,它还需要 rgeos 包:

编辑现在视觉疼痛减少 10%

EDIT 2 现在为东 西半部添加质心

library(rgeos)
library(RColorBrewer)

# Get centroids of countries
theCents <- coordinates(world.map)

# extract the polygons objects
pl <- slot(world.map, "polygons")

# Create square polygons that cover the east (left) half of each country's bbox
lpolys <- lapply(seq_along(pl), function(x) {
  lbox <- bbox(pl[[x]])
  lbox[1, 2] <- theCents[x, 1]
  Polygon(expand.grid(lbox[1,], lbox[2,])[c(1,3,4,2,1),])
})

# Slightly different data handling
wmRN <- row.names(world.map)

n <- nrow(world.map@data)
world.map@data[, c("growth", "category")] <- list(growth = 4*runif(n),
                 category = factor(sample(1:5, n, replace=TRUE)))

# Determine the intersection of each country with the respective "left polygon"
lPolys <- lapply(seq_along(lpolys), function(x) {
  curLPol <- SpatialPolygons(list(Polygons(lpolys[x], wmRN[x])),
    proj4string=CRS(proj4string(world.map)))
  curPl <- SpatialPolygons(pl[x], proj4string=CRS(proj4string(world.map)))
  theInt <- gIntersection(curLPol, curPl, id = wmRN[x])
  theInt
})

# Create a SpatialPolygonDataFrame of the intersections
lSPDF <- SpatialPolygonsDataFrame(SpatialPolygons(unlist(lapply(lPolys,
  slot, "polygons")), proj4string = CRS(proj4string(world.map))),
  world.map@data)

##########
## EDIT ##
##########
# Create a slightly less harsh color set
s_growth <- scale(world.map@data$growth,
  center = min(world.map@data$growth), scale = max(world.map@data$growth))
growthRGB <- colorRamp(c("red", "blue"))(s_growth)
growthCols <- apply(growthRGB, 1, function(x) rgb(x[1], x[2], x[3],
  maxColorValue = 255))
catCols <- brewer.pal(nlevels(lSPDF@data$category), "Pastel2")

# and plot
plot(world.map, col = growthCols, bg = "grey90")

plot(lSPDF, col = catCols[lSPDF@data$category], add = TRUE)

也许有人可以使用ggplot2 提出一个好的解决方案。但是,基于this answer 对单个图形的多个填充比例的问题(“你不能”),如果没有分面,ggplot2 解决方案似乎不太可能(这可能是一个好方法,如上面 cmets 中所建议的那样)。


重新编辑:将两半的质心映射到某物:东(“左”)半的质心可以通过

获得
coordinates(lSPDF)

可以通过以类似方式创建rSPDF 对象来获得西(“右”)半部分:

# Create square polygons that cover west (right) half of each country's bbox
rpolys <- lapply(seq_along(pl), function(x) {
  rbox <- bbox(pl[[x]])
  rbox[1, 1] <- theCents[x, 1]
  Polygon(expand.grid(rbox[1,], rbox[2,])[c(1,3,4,2,1),])
})

# Determine the intersection of each country with the respective "right polygon"
rPolys <- lapply(seq_along(rpolys), function(x) {
  curRPol <- SpatialPolygons(list(Polygons(rpolys[x], wmRN[x])),
    proj4string=CRS(proj4string(world.map)))
  curPl <- SpatialPolygons(pl[x], proj4string=CRS(proj4string(world.map)))
  theInt <- gIntersection(curRPol, curPl, id = wmRN[x])
  theInt
})

# Create a SpatialPolygonDataFrame of the western (right) intersections
rSPDF <- SpatialPolygonsDataFrame(SpatialPolygons(unlist(lapply(rPolys,
  slot, "polygons")), proj4string = CRS(proj4string(world.map))),
  world.map@data)

然后可以根据lSPDFrSPDF的质心在地图上绘制信息:

points(coordinates(rSPDF), col = factor(rSPDF@data$REGION))
# or
text(coordinates(lSPDF), labels = lSPDF@data$FIPS, cex = .7)

【讨论】:

  • 感谢您的出色回答(以及更新)。如果我遵循以下网站上的建议,那是否允许我结合您所做的,但对于 ggplot2? github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
  • @XuWang,您应该能够使用链接的指令绘制lSPDFrSPDF shapefile,但是如果您想要每个fill 映射不同,您将遇到问题一半。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
相关资源
最近更新 更多