如何有效地绘制河流?

时间:2018-07-13 22:26:49

标签: r ggplot2 gis

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

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Anton Game</title>
    <link rel="stylesheet" href="./antonvers.css">
</head>
<body>
    <div id="game">
        <div id="player">
            <h2>
                <span id="pShow">Player:</span>
                <span id="pColor">X</span>
            </h2>
        </div>
        <table id="board" cellpadding="0" cellspacing = "0">
            <tr>
                <td data-value="0"><div id="cell-1"></div></td>
                <td data-value="0">
                    <div id="cell-2"></div>
                </td>
                <td data-value="0">
                    <div id="cell-3"></div>
                </td>
            </tr>
            <tr>
                <td data-value="0">
                    <div class="cell-4"></div>
                </td>
                <td data-value="0">
                    <div class="cell-5"></div>
                </td>
                <td data-value="0">
                    <div class="cell-6"></div>
                </td>
            </tr>
            <tr>
                <td data-value="0">
                    <div class="cell-7"></div>
                </td>
                <td data-value="0">
                    <div class="cell-8"></div>
                </td>
                <td data-value="0">
                    <div class="cell-9"></div>
                </td>
            </tr>
        </table>
        <div id="resetButton">
            <button>Reset</button>
        </div>
    </div>
    <script src="./antonvers.js"></script>
</body>

</html>

enter image description here

2 个答案:

答案 0 :(得分:2)

如果您常规使用shapefile,geom_path和geom_polygon会提供您所需的一切。在最新版本中,ggplot直接处理空间对象,因此不需要使用强化和合并(可能会花费更多时间在代码中)。这是一个使用shapefile of federative units of Brazil from IBGE作为底图的示例:

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()

enter image description here

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

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

enter image description here

答案 1 :(得分:1)

我将执行以下操作:

library(sf)
library(ggplot2)

world_map <- map_data('world')
sdf <- read_sf("PrincipaisRiosDoBrasil.shp")

myMap3 <- ggplot() +
    geom_map(data = world_map, map = world_map, aes(map_id = region), color = 'black', fill = NA, linetype=2) +
    geom_sf(data = sdf)+
    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

enter image description here

您需要将geom_sf的ggplot2更新为3.0.0。