使用ggplot2的facet_wrap功能在地理点上绘制图形

3

我想知道是否过分要求ggplot。 我正在尝试使用facet_grid将某些图形绘制在它们所关联的地理区域上。 结果似乎是正确的,但存在一些问题,例如正确调整比例、删除未使用的网格框等。 不知道有没有人知道如何优化它以使其正常工作?

该示例基于内置的美国州数据集。

任何帮助都会受到赞赏!

    # My failed attempt to regional summary graphs plotted over the region centroid (to overlay onto map):
# example based on the States dataset

require(ggplot2)

# Approx centroid coordinates for the US regions - I created these as the information is missing from the dataset
lat <- c(32L, 42L, 42L, 32L, 42L, 42L, 43L, 32L, 32L, 32L, 42L, 42L, 43L, 43L, 43L, 43L, 32L, 32L, 43L, 32L, 43L, 43L, 43L, 32L, 43L, 42L, 43L, 42L, 43L, 43L, 42L, 43L, 32L, 43L, 43L, 32L, 42L, 43L, 43L, 32L, 43L, 32L, 32L, 42L, 43L, 32L, 42L, 32L, 43L, 42L)
lon <- c(-88L, -114L, -114L, -88L, -114L, -114L, -73L, -88L, -88L, -88L, -114L, -114L, -91L, -91L, -91L, -91L, -88L, -88L, -73L, -88L, -73L, -91L, -91L, -88L, -91L, -114L, -91L, -114L, -73L, -73L, -114L, -73L, -88L, -91L, -91L, -88L, -114L, -73L, -73L, -88L, -91L, -88L, -88L, -114L, -73L, -88L, -114L, -88L, -91L, -114L)

region.centres <- data.frame(lon = lon, lat = lat)

state.x77 <- data.frame(state.x77)
state.x77$State <- row.names(state.x77)
region <- data.frame(Region = as.character(state.region))
data <- data.frame(c(data.frame(state.x77), region, region.centres))

# I can plot a random summary graph for each region:

p <- ggplot(data, aes(Income, colour = 'red')) +
  geom_density(alpha = 0.2)

# But trying to plot onto centroid nearly works but results in loss of geographical positional accuracy
# and includes various graphs that fill the empty grid slots but are not necessary
p + facet_grid(lat ~ lon, space = "free", drop=T)

# How can I get uniform discrete graphs to sit over the centroid for each region?
1个回答

0

或许注释比分面更适合您的用例。在您自己的代码之后尝试以下操作。注释需要大量手动调整,但应该能够完成工作。

library(maps)
states = data.frame(map("state", plot=FALSE)[c("x", "y")])
data.south = subset(data, lon == -88 & lat == 32)
overlay = qplot(data.south$Income, geom="density", color="red")
plot = (ggplot(states, aes(x, y))
        + geom_path()
        + annotation_custom(grob=ggplotGrob(overlay),
                            xmin=-88-2,
                            xmax=-88+2,
                            ymin=32-6,
                            ymax=32+6))

print(plot)

annotation_custom 函数在 ggplot2 0.9.0 版本中被添加。请参阅

http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf


我认为你是正确的 @otsaw,这可能是最好的策略。感谢有用的解决方法。 - geotheory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接