我正在努力产生一个面图/格图的各分布图,每个图显示不同的模型运行如何影响一个变量被映射到多个多边形。问题在于输出图形会在每个绘图中的多边形之间产生奇怪的线条(请参见下图)。ggplot2面shapefile多边形的产生奇怪的线
虽然我已经操纵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, [email protected], 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会话中,并无误地运行它(除了错误,您当然需要帮助......) – bdemarest 2015-02-11 23:06:17