简体   繁体   English

展开列表的data.frame列

[英]Expand data.frame column of lists

I have found myself with a data.frame containing a column of lists. 我发现自己有一个包含一列列表的data.frame。 Within these lists are the coordinates for a polygon; 在这些列表中是多边形的坐标。 the remainder of the columns are the features of the polygons. 其余的列是多边形的要素。

I would like to "spread out" the coordinate points in the lists so that I can use ggplot2 to plot the polygons and make a map, but am having trouble figuring out how to do this. 我想“散布”列表中的坐标点,以便可以使用ggplot2绘制多边形并制作地图,但是在弄清楚如何做到这一点时遇到了麻烦。

I don't really understand how the lists are structured and they are all different sizes, as the polygons have different shapes. 我不太了解列表的结构,它们的大小各不相同,因为多边形的形状不同。

The data was originally a json file. 数据最初是一个json文件。 I pulled it in with jsonlite package and then tried to boil it down to the relevant components like so: 我用jsonlite包将其拉jsonlite ,然后尝试将其简化为相关组件,如下所示:

library(jsonlite)

json_file <- "https://raw.githubusercontent.com/OpenOil-UG/concessionsmap/master/concessions/data/NG_contracts%2B.geojson" json_data <- jsonlite::fromJSON(json_file, simplifyDataFrame=T) data <- json_data$features data2 <- flatten(data)

This may not be the right approach starting with the json file. 从json文件开始,这可能不是正确的方法。 It is just what seemed most straightforward to me as far as taking the data from json to a data.frame I could understand. 就将数据从json移到我能理解的data.frame而言,这对我而言似乎是最直接的。

*Edited to include real data instead of a sample *经过编辑以包括真实数据而不是样本

I would like to [...] use ggplot2 to plot the polygons and make a map 我想使用ggplot2绘制多边形并制作地图

One option: 一种选择:

library(geojsonio)
library(ggplot2)
download.file(json_file, tf <- tempfile(fileext = ".geojson"))
df <- fortify(geojson_read(tf, what="sp"))
ggplot(df, aes(long, lat, group=group)) + 
  geom_polygon(color="white")

That gives you: 那给你:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM