本文代码已经上传至https://github.com/iMetaScience/iMetaPlot如果你使用本代码,请引用:Changchao Li. 2023. Destabilized microbial networks with distinct performances of abundant and rare biospheres in maintaining networks under increasing salinity stress. iMeta 1: e79. https://onlinelibrary.wiley.com/doi/10.1002/imt2.79
代码翻译及注释:农心生信工作室
写在前面
地图及采样点信息绘制在微生物组与环境影响因素相关性研究中非常普遍,其中利用R语言实现环境信息在地图上的可视化,用热图呈现环境信息与地理距离之间的相关性。
本期我们挑选2023年1月9日刊登在iMeta上的Destabilized microbial networks with distinct performances of abundant and rare biospheres in maintaining networks under increasing salinity stress,以文章中Figure 1A为例,讲解和探讨如何基于geoviz, sf, ggplot2实现环境信息在地图上的可视化,先上原图:
代码、数据和结果下载,请访问https://github.com/iMetaScience/iMetaPlot/tree/main/230406EnvFactorsOnMap
接下来,我们将通过详尽的代码逐步拆解原图,最终实现对原图的复现。
R包检测和安装
01
安装核心R包geoviz,sf(读取和保存空间矢量数据),ggplot2以及一些功能辅助性R包,并载入所有R包。
# 列出所有需要的R包
package.list=c("geoviz","tidyverse","sf","terra","rasterVis",
"ggspatial","rgdal","rnaturalearth","rnaturalearthdata",
"raster","RColorBrewer")
# 循环检查package.list中所有所需包并加载包,如没有则安装后加载
for (package in package.list) {
if(!require(package,character.only=T,quietly=T)){
install.packages(package)
library(package,character.only = T)
}
}
# 部分包安装可能会报错,可尝试将分别安装,其中geoviz、sf和terra包可通过devtools安装
# 检查开发者工具devtools,如没有则安装
if (!require("devtools"))
install.packages("devtools")
library(devtools)
# 检查geoviz包,没有则通过github安装最新版
if(!require("geoviz"))
install_github("neilcharles/geoviz", force = TRUE)
library(geoviz)
# 如果无法打开github使用install_github()函数,可多次尝试从https://github.com/neilcharles/geoviz下载zip包后离线手动安装
# 运行devtools::install_local("C:/Users/TD/Desktop/geoviz.zip"),其中C:/Users/TD/Desktop部分需对应自己电脑中下载好的zip位置,其余包的安装均可参照此方法。
# 检查sf包,没有则通过github安装最新版
if(!require("sf"))
install_github("r-spatial/sf", force = TRUE)
library(sf)
# sf包安装依赖GDAL (>= 2.0.1), GEOS (>= 3.4.0) and Proj.4 (>= 4.8.0)
# 如果环境中存在多版本的GDAL 等会使sf安装报错
# 解决办法是在编译过程中正确链接库文件,出现库文件冲突时,运行unlink("D:/Tool/R_Library/00LOCK", recursive = TRUE)
# 把报错文件删掉,其中D:/Tool/R_Library/00LOCK是对应自己电脑报错显示的位置;当出现镜像设置报错时,可尝试直接从R-base安装,设置合适镜像位置。
# 检查terra包,没有则通过github安装最新版
if(!require("terra"))
install_github("rspatial/terra", force = TRUE)
library(terra)
读取数据
02
添加地图绘制需要的矢量数据,及相关环境因子的标注数据。示例数据可在GitHub上获取。
# 利用::使用在sf包中的read_sf函数提取地图的矢量数据
shp1 <- sf::read_sf("shandong.json")
# 读取图上标注点的坐标数据,在GitHub上获取数据
geo <- read.csv("geodata.csv", row.names = 1)
在地图上标注相关信息
03
开始画图,先绘制地图。
# 预设渐变色,颜色来自RColorBrewer包
color2 = colorRampPalette(c("#EFF3FF", "#08519C"))(66)
p1a_1 <- ggplot() +
geom_sf(data = shp1) + # 快速绘制地图
annotation_scale(location = "br") + # 设置距离刻度尺
labs(x = NULL,y = NULL) +
geom_sf(data = shp1, fill = "white", size = 0.4, color = "grey") + # 添加地图边界
xlim(120,121.2) + # 设置x轴范围
ylim(36,36.7) # 设置y轴范围
p1a_1
04
在地图上添加环境因子的标注。
p1a_2 <- p1a_1 +
geom_point(data = geo, aes(lon, lat, fill = salinity),
size = 1.8, alpha = 1, shape = 21, stroke = 0.2) +
scale_fill_gradientn(colours = color2) # 按salinity值填充渐变色,通过颜色的变化可以看出环境因子与地理距离之间的变化关系
p1a_2
05
对整体进行细节美化。
p1a_3 <- p1a_2 +
theme_bw() + # 预设主题
theme(panel.grid.major = element_blank(),# 去除主题自带的主要网格线
panel.grid.minor = element_blank(),# 去除主题自带的次要网格线
# panel.background = element_blank(),# 去除主题本身设置的背景
panel.background = element_rect(fill = "#CADEF0", color = "gray71", size = 0.5),# 对图形背景色调整
legend.title = element_blank(),
# plot.background = element_rect(fill="#CADEF0", color="gray71",size = 0.5), #对图形背景色调整
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
legend.position = c(0.9, 0.3),#图例位置设置
legend.background = element_rect(fill = F),
legend.key.size = unit(12, "pt"))#图例刻度设置
p1a_3
06
添加指北针。
p1a_4 <- p1a_3 +
annotation_north_arrow(location = "tl",#调整指北针位置,
style = north_arrow_fancy_orienteering(
fill = c("grey40", "white"),
line_col = "grey20")) #对指北针颜色进行调整
p1a_4
完整代码
#列出所有需要的R包
package.list=c("geoviz","tidyverse","sf","terra","rasterVis","ggspatial","rgdal","rnaturalearth","rnaturalearthdata","raster")
#循环检查package.list中所有所需包并加载包,如没有则安装后加载
for (package in package.list) {
if(!require(package,character.only = T, quietly = T)){
install.packages(package)
library(package,character.only = T)
}
}
#读取矢量数据
shp1 <- sf::read_sf("shandong.json")
#读取图上标注点的坐标数据
geo <- read.csv("geodata.csv", row.names = 1)
#预设渐变色,颜色来自RColorBrewer包
color2 = colorRampPalette(c("#EFF3FF", "#08519C"))(66)
p1a <-ggplot()+
geom_sf(data = shp1)+ #快速绘制地图
annotation_scale(location = "br")+ #设置距离刻度尺
labs(x = NULL, y = NULL)+
geom_sf(data = shp1, fill = "white", size = 0.4, color = "grey")+ # 添加地图边界
xlim(120,121.2)+ # 设置x轴范围
ylim(36,36.7)+ # 设置y轴范围
# 在图上添加散点的标注
geom_point(data = geo, aes(lon, lat, fill = salinity),
size = 1.8, alpha = 1, shape = 21, stroke = 0.2) +
scale_fill_gradientn(colours = color2)+ #按salinity值填充渐变色
theme_bw()+ # 预设主题
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_rect(fill = "#CADEF0", color = "gray71", size = 0.5), # 去除主题本身设置的背景
legend.title = element_blank(),
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
legend.position = c(0.9, 0.3),
legend.background = element_rect(fill = F),
legend.key.size = unit(12, "pt"))+
# 添加指北针
annotation_north_arrow(location="tl", # 调整指北针位置,
style = north_arrow_fancy_orienteering(
fill = c("grey40", "white"),
line_col = "grey20")) # 对指北针颜色进行调整
p1a
ggsave('环境因子在地图上的可视化.pdf', p1a, height = 8, width = 8)
以上数据和代码仅供大家参考,如有不完善之处,欢迎大家指正!
更多推荐
(▼ 点击跳转)
iMeta | 德国国家肿瘤中心顾祖光发表复杂热图(ComplexHeatmap)可视化方法
iMeta | 浙大倪艳组MetOrigin实现代谢物溯源和肠道微生物组与代谢组整合分析
1卷1期
1卷2期
1卷3期
1卷4期
2卷1期
期刊简介
“iMeta” 是由威立、肠菌分会和本领域数百位华人科学家合作出版的开放获取期刊,主编由中科院微生物所刘双江研究员和荷兰格罗宁根大学傅静远教授担任。目的是发表原创研究、方法和综述以促进宏基因组学、微生物组和生物信息学发展。目标是发表前10%(IF > 15)的高影响力论文。期刊特色包括视频投稿、可重复分析、图片打磨、青年编委、前3年免出版费、50万用户的社交媒体宣传等。2022年2月正式创刊发行!
联系我们
iMeta主页:http://www.imeta.science
出版社:https://onlinelibrary.wiley.com/journal/2770596x
投稿:https://mc.manuscriptcentral.com/imeta
邮箱:office@imeta.science