代码之家  ›  专栏  ›  技术社区  ›  Ferroao

如何有效地规划河流?

  •  0
  • Ferroao  · 技术社区  · 8 年前

    我想出了一个用风水道绘制河流的方法。我不知道有没有更好的方法。我不得不把数据框架分成数百条“河流”。所以很慢。有什么想法吗?

    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
    

    enter image description here

    2 回复  |  直到 8 年前
        1
  •  2
  •   Carlos Eduardo Lagosta    8 年前

    如果您经常使用shapefiles,geom_path和geom_polygon会提供您需要的一切。在最近的版本中,ggplot直接处理空间对象,因此不需要使用fortify和merge(这一步骤可能会占用更多的代码时间)。下面是一个使用 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

        2
  •  1
  •   AndS.    8 年前

    我将执行以下操作:

    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

    您需要将ggplot2更新为3.0.0 geom_sf .