我认为你可以得到(有效)两种不同的填充尺度,与scale_fill_brewer和scale_fill_manual的小黑客。
这里是我的输出:
我使用的代码的第一位从你的问题贴在其他线程:
library(rgdal)
library(ggplot2)
library(maptools)
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
# 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([email protected])
[email protected][, 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))),
[email protected])
现在我的贡献(借用用户名全/半!hrbrmstr)
# get two data.frames, one with whole countries and the other with the left half
# this relies on code from SO user BenBarnes
whole <- fortify(world.map, region="ISO3")
half <- fortify(lSPDF, region="ISO3")
# random growth/category data, similar to the random data originally
# suggested by Xu Wang
set.seed(123)
df <- data.frame(id = unique([email protected]$ISO3),
growth = 4*runif(n),
category = factor(sample(letters[1:5], n, replace=T)))
# make growth a factor; 5 levels for convenience
df$growth_fac <- cut(df$growth, 5)
# append growth and category factor levels together
growth_cat_levels <- c(levels(df$category), levels(df$growth_fac))
# adjust factors with new joint levels
df$growth_fac <-
factor(df$growth_fac, levels=growth_cat_levels)
df$category <-
factor(df$category, levels=growth_cat_levels)
# create a palette with some sequential colors and some qualitative colors
pal <- c(scale_fill_brewer(type='seq', palette=6)$palette(5),
scale_fill_brewer(type='qual', palette='Pastel2')$palette(5))
# merge data
whole <- data.frame(merge(whole, df, by='id'))
half <- data.frame(merge(half, df, by='id'))
# plot
ggplot() +
geom_polygon(data=whole,
aes(x=long, y=lat, group=group, fill=growth_fac),
color='black', size=0.15) +
geom_polygon(data=half,
aes(x=long, y=lat, group=group, fill=category),
color=NA) +
scale_shape_discrete() +
coord_equal() +
scale_fill_manual('Category, Growth',
values=pal, breaks=growth_cat_levels) +
guides(fill=guide_legend(ncol=2))
一些注意事项:
- 我仍然认为这是一本难以阅读的地图,但有趣的挑战
- 我将'category'的名称从数字更改为alpha,以避免与'增长'数据混淆。
- 我还保留了cut的“增长”数据标签,以帮助明确说明这是分级连续数据。
- 起初,我在图例左侧有增长颜色,但我换了一下;由于类别决定了左侧国家多边形的填充颜色,我认为类别应该出现在图例左侧
- 我尝试了几个不同的调色板选项。一种危险是定性量表的颜色与顺序量表的范围太相似了(就像我在编辑之前发表的帖子一样)。有一面灰度和一面颜色有助于避免这种情况
所以你想要达到与原始问题相同的结果? – Konrad
我想要达到与*答案*相同的结果(问题已经使用ggplot2,答案不会)。 –
你会遇到一些问题,因为如果几乎不可能有多个不同的多边形填充颜色比例 – hrbrmstr