如何有效地绘制河流? [英] how to plot rivers efficiently?

查看:105
本文介绍了如何有效地绘制河流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出了一种使用geom_path绘制河流的方法.我不知道是否有更好的方法.我不得不将数据框分成数百个河流".所以它很慢.有什么想法吗?

I came up with a way to plot rivers using geom_path. I do not know if there is a better way. I had to split the dataframe into hundreds of "rivers". So it is very slow. Any ideas?

world_map <- map_data('world') 
system("wget https://sites.google.com/site/joabelb/Home/PrincRiosBrazil.zip")
system("unzip -o PrincRiosBrazil.zip")
library(rgdal)
shapeHid <- readOGR(dsn = ".", layer = "PrincipaisRiosDoBrasil") 
shapeHid@data$id = rownames(shapeHid@data)
library(ggplot2)
shapeHid.points = fortify(shapeHid, region="id")#
shapeHid.df = merge(shapeHid.points, shapeHid@data, by="id", all=F)

listofrivers<-split(shapeHid.df, shapeHid.df$id)

myMap3 <- ggplot() +
  lapply(listofrivers, function(x) geom_path(data=x, aes(x=long, y=lat), color="gray70", linetype=1)) +
  geom_map(data = world_map, map = world_map, aes(map_id = region),
           color = 'black', fill = NA, linetype=2) +
  theme(panel.border = element_rect(fill = NA, colour = "black"))+
  theme(axis.title=element_blank())+
  scale_y_continuous(limits=c(-15,6),expand=c(0,0))+
  scale_x_continuous(limits=c(-76,-55),expand=c(0,0))
myMap3

推荐答案

如果您常规使用shapefile,geom_path和geom_polygon会提供您所需的一切.在最新版本中,ggplot直接处理空间对象,因此不需要使用强化和合并(可能会花费更多时间在代码中).以下是使用形状单位的单位的示例来自IBGE的巴西作为底图:

If you routinely work with shapefiles, geom_path and geom_polygon gives everything you need. In recent versions, ggplot deals directly with spatial objects, so there's no need to use fortify and merge (probably the step taking more time in your code). Here's an example using the shapefile of federative units of Brazil from IBGE as base map:

shapeUFs <- readOGR('.', 'BRUFE250GC_SIR')
shapeHid <- readOGR('.', 'PrincipaisRiosDoBrasil') 

ggplot(shapeUFs, aes(long, lat, group = group)) +
  geom_polygon(fill = 'gray90', color = 'black') +
  geom_path(data = shapeHid, color = 'steelblue2') +
  coord_map() + theme_void()

与ggplot中使用的几何图形相比,形状尺寸(取决于特征数量和详细程度)对性能的影响更大.您可以使用rgeos :: gSimplify减少空间多边形/线对象中的顶点数量.您也可以在地图上直接绘制点:

Performance will be affected by the size of your shapes (determined by number of features and level of details) more than the geometry you're using in ggplot. You can use rgeos::gSimplify to reduce the number of vertices in a spatial polygon/lines object. You can also plot points directly over the map:

# Simplifying the geometry of the federative units
shapeUFs.s <- rgeos::gSimplify(shapeUFs, .05, TRUE)

# Storing map in an object
riversMap <- ggplot(shapeUFs.s, aes(long, lat)) +
  geom_polygon(aes(group = group), fill = 'gray90', color = 'black') +
  geom_path(data = shapeHid, aes(group = group), color = 'steelblue2') +
  coord_map() + theme_void()

# Sampling 20 cities in Brazil
brMunics <- read.csv('https://raw.githubusercontent.com/kelvins/Municipios-Brasileiros/master/Municipios_Brasileiros.csv')
Munics <- brMunics[sample(nrow(brMunics), 20), ]

# Plotting points over the map
riversMap + geom_point(data = Munics, aes(Longitude, Latitude), color = 'red')

# If your data already have the coordinates named 'lat' and 'long',
# you can skip aes(Longitude, Latitude):
names(Munics)[6:7] <- c('lat','long')
riversMap + geom_point(data = Munics, color = 'red')

这篇关于如何有效地绘制河流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆