【问题标题】:Error exporting ggplot map to KML将 ggplot 地图导出到 KML 时出错
【发布时间】:2015-12-28 20:49:38
【问题描述】:

我想知道是否有人修复了 GIS 转换(也提前感谢您对其他问题的宝贵建议)。我正在尝试将 choropleth 地图从 R 导出为 KML 格式以放置在谷歌地图中。我必须制作很多这样的地图,所以希望 R 能比 ArcGIS 更快地处理它们(尽管如果你也有小费,那很有帮助;我是两者的普通用户)。我能够拼凑出一个不错的 ggplot choropleth(以下并感谢 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]

【问题讨论】:

  • 恐怕 ggplot2 会产生“grob”,而不是 R 中的空间工具能够转换的东西。如果您执行ggplot_build,您将获得一个列表结构,其中包含多边形的数据(在数据框中),您可以将其转换为SpatialPolygons,然后再转换为KML。

标签: r google-maps ggplot2 gis kml


【解决方案1】:

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

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

上面的实现如下:

# 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"))

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2015-03-07
    • 1970-01-01
    相关资源
    最近更新 更多