将ggplot映射导出为KML时出错

时间:2015-12-28 20:49:39

标签: r google-maps ggplot2 gis kml

我想知道是否有人修复了GIS转换(提前感谢您对其他问题的宝贵建议)。我正在尝试将一个等值区域地图从R导出到KML格式以放置在谷歌地图中。我必须制作很多这些地图,所以希望R比ArcGIS更快地处理它们(尽管如果你有一个提示,那也是有用的;我是两者的适度用户)。我能够拼凑一个漂亮的ggplot等值线(以下并感谢this),但是当我导出到KML时,它会出现以下错误:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘is.projected’ for signature ‘"gg"’

在这里,我试图在多边形“病房”上绘制不同的“emanp”值。这是我的代码。

library(rworldmap)
library(foreign)
library(maptools)
library(rgdal)
library(shapefiles)
library(ggplot2)
library(ggmap)
library(plotKML)

#read in attributes
mayor2015<-read.dta("WardsMayor2015.dta")
#read in spatial data
wardmap<-readOGR("WARDS_2015.shp", layer="WARDS_2015")

#merge attributes and spatial
wardmap@data= data.frame(wardmap@data, mayor2015[match(wardmap@data[,"WARD"], mayor2015[,"WARD"]),])
    #fortify spatial
wardmap.f<-fortify(wardmap, region="WARD")
wardmap.f <- merge(wardmap.f, wardmap@data, by.x = "id", by.y="WARD")
#Map it
Map <- ggplot(wardmap.f, is.projected=TRUE, aes(long, lat, group=group, fill      =emanp15)) + geom_polygon() + 
    coord_equal() + labs(x = NULL, y = NULL, fill = "%") + 
    ggtitle("Support")
Map + scale_fill_gradient(low = "white", high = "black")

所有似乎都通过ggplot命令工作。当我使用writeOGR时,它会给出上面关于继承方法的错误。

#export to KML
writeOGR(Map, layer="ChiWards.kml", driver="KML", dataset_options=c("NameField=WARD"))
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘is.projected’ for signature ‘"gg"’

如果我对非强化数据使用writeOGR,则相同。我是否需要垃圾ggplot,或者我的投影设置是否错误,或者我只是将太多不同的来源放在一起?

以下是空间数据“wardmap”的信息

    > summary(wardmap)
Object of class SpatialPolygonsDataFrame
Coordinates:
      min     max
x 1091131 1205198
y 1813892 1951669
Is projected: TRUE 
proj4string :
[+proj=tmerc +lat_0=36.66666666666666 +lon_0=-88.33333333333333
+k=0.9999749999999999 +x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs
+ellps=GRS80 +towgs84=0,0,0]

1 个答案:

答案 0 :(得分:0)

这个问题发布已经三年了,但是我想我会分享我的想法。您可以将ggplot转换为raster对象,如下所示:

  1. 使用ggplottiff图保存到任何格式的图像文件中(我会使用ggsave
  2. 使用raster(或raster用于彩色图像)为保存的图像创建stack对象

上述实现如下:

# You may need to remove the margins from the plot
Map <- Map + 
  theme(    
        axis.ticks=element_blank(), 
        axis.text.x=element_blank(), 
        axis.text.y=element_blank(), 
        axis.title.x=element_blank(), 
        axis.title.y=element_blank(),
        plot.margin = unit(c(0, 0, 0, 0), "null"),
        legend.position = 'none'
       ) +
       labs(x=NULL, y=NULL)

# Save the plot
ggsave(plot=Map, "my_ggplot.tiff", device = "tiff")

# Create a StackedRaster object from the saved plot
rasterMap <- stack("my_ggplot.tiff")

# Get the GeoSpatial Components
lat_long <- ggplot_build(Map)$layout$panel_params[[1]][c("x.range","y.range")] 

# Supply GeoSpatial  data to the StackedRaster 
extent(rasterMap) <- c(lat_long$x.range,lat_long$y.range)
projection(rasterMap) <- CRS("+proj=longlat +datum=WGS84")

# Create the KML
writeOGR(rasterMap, layer="ChiWards.kml", driver="KML", dataset_options=c("NameField=WARD"))

希望有帮助。