R语言 如何用ggplot2制作世界地图

R语言 如何用ggplot2制作世界地图

在这篇文章中,我们将讨论如何使用R编程语言创建一个世界地图并在上面绘制数据。

为了使用它创建世界地图,我们将使用R语言的ggplot2包的geom_map()函数。这个函数返回一个ggplot对象,所以所有在其他ggplot图上工作的函数也会在geom_map()中工作。

语法

ggplot() + geom_map( data, map, aes() )

其中。

  • data: 决定在该层显示的数据。
  • map: 决定了包含地图坐标的数据框架
  • aes(): 决定美学,即坐标系变量和其他美学方面。

例子

这里,是一个使用geom_map()函数绘制的基本世界地图。使用map_data()函数获取地图坐标的数据框。这就创建了一个世界地图的ggplot对象,它可以被格式化、修改,并像其他的ggplot2绘图一样作为一个图层进行绘制。

# load library tidyverse
library(tidyverse)
  
# create data for world coordinates using 
# map_data() function
world_coordinates <- map_data("world")
  
# create world map using ggplot() function
ggplot() +
  
# geom_map() function takes world coordinates 
# as input to plot world map
  geom_map(
    data = world_coordinates, map = world_coordinates,
    aes(long, lat, map_id = region)
  )
Bash

输出

这是一张基本的地图,采用ggplot2软件包的默认颜色方案。

如何用R语言中的ggplot2制作世界地图?

颜色定制

我们可以使用geom_map()函数的颜色、填充和大小参数来定制绘图的视觉效果。

语法

ggplot() + geom_map( data, map, aes(), size, color, fill )

其中。

  • size: 决定了地图边界的厚度
  • color: 决定了地图边界的颜色
  • fill : 决定了地图背景的填充颜色。

例子

这里,是一张使用geom_map()函数的填充、颜色和大小属性制作的具有绿色填充和白色厚边框的世界地图。这使得地图在视觉上更加吸引人,并且由于白色边界的对比,帮助我们区分不同的国家。

# load library tidyverse
library(tidyverse)
  
# create data for world coordinates using 
# map_data() function
world_coordinates <- map_data("world")
  
# create world map using ggplot() function
ggplot() +
  
# geom_map() function takes world coordinates as input 
# to plot world map color parameter determines the 
# color of borders in map fill parameter determines 
# the color of fill in map size determines the thickness
# of border in map
  geom_map(
    data = world_coordinates, map = world_coordinates,
    aes(long, lat, map_id = region)
    color = "white", fill = "darkgreen", size = 0.2
  )
Bash

输出

如何用R语言中的ggplot2制作世界地图?

在世界地图上绘制数据

由于geom_map()函数返回的是ggplot()对象,我们可以在上面绘制数据,就像在其他的ggplot图上一样。要做到这一点,我们只需要在我们的数据集中有纬度和经度数据,就可以在地图上绘制数据。我们可以使用所需的ggplot2函数在世界地图上绘制直线图、散点图、boxplot或任何其他需要的图。

语法

ggplot()+ geom_map()+ geom_plot()

其中。

  • geom_plot(): 决定了任何来自ggplot2软件包的绘图函数。

例子

在这个例子中,我们使用geom_point()函数在geom_map()函数的基础上绘制了一个火山爆发的数据集。这里我们可以显示世界各地火山爆发强度的变化。这有助于我们了解火山爆发的关键区域。

# load library tidyverse
library(tidyverse)
  
# create data for world coordinates using map_data() function
world_coordinates <- map_data("world")
  
# read volcano_eruption data from volcano.csv
volcano_eruption <- readr::read_csv("volcano.csv")
  
# create world map using ggplot() function
ggplot() +
# geom_map() funct
ion takes world coordinates as input
# to plot world map color parameter determines the
# color of borders in map fill parameter determines the
# color of fill in map size determines the thickness of
# border in map
  geom_map(
    data = world, map = world,
    aes(long, lat, map_id = region),
    color = "green", fill= "lightyellow"
  )+
# geom_point function is used to plot scatter plot on top 
# of world map
geom_point(
    data = volcano_eruption,
    aes(longitude, latitude, color = primary_volcano_type,
        size=population_within_10_km),
    alpha = 1
  ) +
  
# legend.position as none removes the legend
theme(legend.position="none")
Bash

输出

如何用R语言中的ggplot2制作世界地图?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册