【问题标题】:ggplot2 facet plot of shapefile polygons produces strange linesshapefile多边形的ggplot2平面图产生奇怪的线条
【发布时间】:2015-02-11 21:40:47
【问题描述】:

我正在制作一个等值线图的小平面/点阵图,每个图都显示了不同的模型运行如何影响一个变量,该变量映射到多个多边形。问题是输出图形会在每个绘图的多边形之间产生奇怪的线条(见下图)。

虽然我已将 shapefile 操作并转换为具有 ggplot2 适当属性的数据框,但我不熟悉如何使用该包的详细信息,并且在线文档仅限于这样一个复杂的包。我不确定是什么参数导致了这个问题,但我怀疑它可能是 aes 参数。

脚本:

library(rgdal, tidyr, maptools, ggplot2, dplyr, reshape2)

setwd('D:/path/to/wd')

waterloo <- read.table("waterloo-data.txt", header=TRUE, sep=',', stringsAsFactors=FALSE)
waterloo <- data.frame(waterloo$DAUID, waterloo$LA0km, waterloo$LA4_exp, waterloo$LA20km, waterloo$LA30km, waterloo$LA40km, waterloo$LA50km)
colnames(waterloo) <- c("DAUID", "LA0km", "LA10km","LA20km", "LA30km", "LA40km", "LA50km")

## Produces expenditure measurements by ID variable DAUID, using reshape2/melt
wtidy <- melt(waterloo, id.vars=c("DAUID"), measure.vars = c("LA0km", "LA10km", "LA20km", "LA30km", "LA40km", "LA50km"))
colnames(wtidy) <- c("DAUID", "BufferSize", "Expenditure")

wtidy$DAUID <- as.factor(wtidy$DAUID) # for subsequent join with wtrl_f

### READ SPATIAL DATA ###
#wtrl <- readOGR(".", "Waterloo_DA_2011_new")
wtrl <- readShapeSpatial("Waterloo_DA_2011_new")
wtrl$id <- row.names(wtrl)

wtrl_f <- fortify(wtrl)
wtrl_f <- left_join(wtrl_f, wtrl@data, by="id")

# Join wtrl fortified (wtrl_f) to either twaterloo or wtidy
wtrl_f <- left_join(wtrl_f, wtidy, by="DAUID")

### PLOT SPATIAL DATA ###
ggplot(data = wtrl_f, # the input data
       aes(x = long.x, y = lat.x, fill = Variable/1000, group = BufferSize)) + # define variables
  geom_polygon() + # plot the DAs
  geom_path(colour="black", lwd=0.05) + # polygon borders
  coord_equal() + # fixed x and y scales
  facet_wrap(~ BufferSize, ncol = 2) + # one plot per buffer size
  scale_fill_gradient2(low = "green", mid = "grey", high = "red", # colors
                       midpoint = 10000, name = "Variable\n(thousands)") + # legend options
  theme(axis.text = element_blank(), # change the theme options
        axis.title = element_blank(), # remove axis titles
        axis.ticks = element_blank()) # remove axis ticks

输出图形如下:

奇怪的!我取得了很好的进展,但我不知道 ggplot 从哪里得到这些线。对此的任何帮助将不胜感激!

PS;作为另一个不相关的问题,多边形线相当参差不齐。我将如何平滑这些线条?

【问题讨论】:

  • 如果你能提供一个最小的、可重现的例子,帮助会容易得多。最小的意思是足够的代码和数据来显示问题。可重现的意思是我可以将您提供的代码完全按照提供的方式粘贴到新的 R 会话中并无错误地运行它(当然,您需要帮助的错误除外......)

标签: r ggplot2 maps shapefile


【解决方案1】:

这个答案帮助我解决了我的问题,但不是在我准备发布这个最小的例子之前。我在这里分享它,以防它帮助某人更快地解决同样的问题。

问题: 我正在尝试使用 ggplot2 在 R 中制作基本地图。多边形填充错误,产生了额外的线条。

library("ggplot2")
library("maps")

map <- ggplot(map_data("world", region = "UK"), aes(x = long, y = lat)) + geom_polygon()
map

wrong map image

解决方案: 我必须设置美学“组”参数以将多边形点按正确的顺序排列,否则 ggplot 将尝试在南海岸中间绘制一片苏格兰海岸线(例如)。

map <- ggplot(map_data("world", region = "UK"), aes(x = long, y = lat, group = group)) + geom_polygon()
map

【讨论】:

    【解决方案2】:

    好的,我设法通过更改 ggplot2 手册第 11 页上的审美组参数来解决此问题: http://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf

    正确的参数是“组”,而不是用于对图进行分组的因子。正确的ggplot代码:

        ggplot(data = wtrl_f, # the input data
           aes(x = long.x, y = lat.x, fill = Expenditure/1000, group = group)) + # define variables
      geom_polygon() + # plot the DAs
      geom_path(colour="black", lwd=0.025) + # DA borders
      coord_equal() + # fixed x and y scales
      facet_wrap(~ BufferSize, ncol = 2) + # one plot per buffer size
      scale_fill_gradient2(low = "green", mid = "grey", high = "red", # colors
                           midpoint = 10000, name = "Expenditures\n(thousands)") + # legend options
      theme(axis.text = element_blank(), # change the theme options
            axis.title = element_blank(), # remove axis titles
            axis.ticks = element_blank()) # remove axis ticks
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2014-06-14
      • 1970-01-01
      • 2019-04-08
      相关资源
      最近更新 更多