更新/编辑/表示:使用相同的图形设备渲染相同的空间数据需要1秒而tmap不是80秒ggplot2,即使tmap绘图的R对象的大小是80倍.内部和/或实施btw的差异.包和图形设备?
library(ggplot2); library(sf);
library(tmap); library(tidyverse)
library(here) # project directory
data(World) # sf object from tmap; Provides Africa polygon
# 'd' data pulled from acleddata.com/data, restricted to Aug 18 2017 - Aug 18 2018, Region: N/S/E/W/Middle Africa only
d <- read.csv(here('2017-08-18-2018-08-18-Eastern_Africa-Middle_Africa-Northern_Africa-Southern_Africa-Western_Africa.csv'))
dsf <- st_as_sf(d, coords = c('longitude', 'latitude'), crs = 4326)
Run Code Online (Sandbox Code Playgroud)
使用的数据:1 - 来自tmap包本身的'世界'shapefile数据,和
2 - acleddata.com/data,2017年 8月18日至2018年8月18日期间限制在非洲的ACLED冲突事件(7.8 MB .csv;这些过滤器:)
绘图渲染:
# ggplot2; build plot, assign to object
dev.cur() # RStudioGD on …Run Code Online (Sandbox Code Playgroud) 我有以下数据帧:
df <- structure(list(country = c("Ghana", "Eritrea", "Ethiopia", "Ethiopia",
"Congo - Kinshasa", "Ethiopia", "Ethiopia", "Ghana", "Botswana",
"Nigeria"), CommodRank = c(1L, 2L, 3L, 1L, 3L, 1L, 1L, 1L, 1L,
1L), topCommodInCountry = c(TRUE, FALSE, FALSE, TRUE, FALSE,
TRUE, TRUE, TRUE, TRUE, TRUE), Main_Commod = c("Gold", "Copper",
"Nickel", "Gold", "Gold", "Gold", "Gold", "Gold", "Diamonds",
"Iron Ore")), row.names = c(NA, -10L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), vars = "country", drop = TRUE, indices = list(
8L, 4L, 1L, c(2L, 3L, 5L, 6L), …Run Code Online (Sandbox Code Playgroud) 在下面的reprex中,我在某些点和多边形数据上运行空间连接,但在使用sp包时使用包时出乎意料地获得了不同的结果sf.为什么是这样?
我试图计算网格方块acled内的点数prio,但是如下所示,我的计数在包之间有所不同,即使运行st_covers连接sf,我应该知道在功能上与使用over方法相同sp.
library(sp) # packageVersion("sp") #> [1] ‘1.2.7’
library(sf) # packageVersion("sf") #> [1] ‘0.6.3’
library(rgdal)
library(maptools)
library(dplyr); library(tibble)
Run Code Online (Sandbox Code Playgroud)
以下是我正在使用的示例数据:
# prio (polygon squares) and acled (points); in both sp and sf objects:
# prio sf polygons object
priosf <- structure(list(
CELL_ID = c(180365, 176783, 150830, 145866, 140055),
gwno = c(615L, 616L, 432L, 626L, 475L),
POP = c(111983.7, 107369.7, 12169.35, 23005.76, 527012.1),
prio_country = c("Algeria", …Run Code Online (Sandbox Code Playgroud)