在模仿中精进数据可视化_使用R语言绘制世界地图上添加采样信息
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
论文出处太多了,这里就不放了,包含大尺度采样数据的Paper基本上都有这种图,并且一般都是Figure1a
。
这里直接上图:
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(scatterpie)
library(readxl)
library(patchwork)
library(ggrepel)
library(ggfun)
加载数据
####----load Data----####
# 首先加载地图数据
world <- map_data("world") %>%
dplyr::mutate(region = str_replace(region, "Taiwan", "China"))
# head(world)
# 加载取样点信息
Sample_Info <- read_xlsx(path = "Sample_Info.xlsx")
可视化
❝版本1
####----Plot----####
# 先绘制世界地图
p <- ggplot(data = world, aes(x = long, y = lat)) +
geom_map(map = world, aes(map_id = region), fill = NA, color = "#000000") +
coord_equal() +
labs(x = "Long", y = "Lat") +
ggtitle(label = "World Map") +
theme_bw() +
theme(
panel.grid = element_blank(),
axis.text = element_text(size = 12, color = "#000000"),
axis.title = element_text(size = 15, color = "#000000"),
legend.background = element_roundrect(color = "#737373"),
plot.title = element_text(size = 15, hjust = 0.5)
)
p
ggsave(filename = "p.pdf",
plot = p,
height = 6,
width = 10)
❝版本2
# 添加采样点
p2 <- p +
geom_point(data = Sample_Info,
aes(x = long, y = lat, size = size, fill = size),
color = "#000000",
shape = 21) +
ggtitle(label = "World Map add Sampling Point") +
geom_text_repel(data = Sample_Info,
aes(x = long, y = lat, label = Name, color = size)) +
scale_fill_gradient(low = "#fcc5c0", high = "#ae017e") +
scale_color_gradient(low = "#fcc5c0", high = "#ae017e")
p2
ggsave(filename = "p2.pdf",
plot = p2,
height = 6,
width = 10)
❝版本3
# 添加扇形图
p3 <- p +
geom_scatterpie(data=Sample_Info, aes(x=long, y=lat, group=region),
cols=paste0("Treatment", 1:4)) +
ggtitle(label = "World Map add Sampling Pie") +
scale_fill_manual(values = c("#7fc97f", "#beaed4", "#fdc086", "#8da0cb")) +
coord_equal()
p3
ggsave(filename = "p3.pdf",
plot = p3,
height = 6,
width = 10)
❝版本4
# 还可以修改扇形的大小
p4 <- p +
geom_scatterpie(data=Sample_Info, aes(x=long, y=lat, group=region, r = size),
cols=paste0("Treatment", 1:4)) +
scale_fill_manual(values = c("#7fc97f", "#beaed4", "#fdc086", "#8da0cb")) +
ggtitle(label = "World Map add Sampling Pie") +
geom_scatterpie_legend(Sample_Info$size, x = -150, y = -50) +
coord_equal()
p4
ggsave(filename = "p4.pdf",
plot = p4,
height = 6,
width = 10)
❝最后是拼图
# 拼图
p_combine <- p/p2/p3/p4
p_combine
ggsave(filename = "Plot.pdf",
plot = p_combine,
height = 15,
width = 10)
版本信息
####----sessionInfo----####
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 14.6.1
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggfun_0.1.5 ggrepel_0.9.6 patchwork_1.2.0.9000 cowplot_1.1.3
[5] readxl_1.4.3 scatterpie_0.2.1 lubridate_1.9.3 forcats_1.0.0
[9] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[13] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] utf8_1.2.4 generics_0.1.3 stringi_1.8.3 hms_1.1.3 magrittr_2.0.3
[6] grid_4.3.0 timechange_0.2.0 maps_3.4.1.1 cellranger_1.1.0 writexl_1.4.2
[11] fansi_1.0.6 scales_1.3.0 tweenr_2.0.3 textshaping_0.3.7 cli_3.6.3
[16] rlang_1.1.4 polyclip_1.10-7 munsell_0.5.1 withr_3.0.1 tools_4.3.0
[21] tzdb_0.4.0 colorspace_2.1-1 vctrs_0.6.5 R6_2.5.1 lifecycle_1.0.4
[26] MASS_7.3-60 ragg_1.2.6 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.5
[31] glue_1.7.0 Rcpp_1.0.13 systemfonts_1.1.0 ggforce_0.4.2 tidyselect_1.2.1
[36] rstudioapi_0.15.0 farver_2.1.2 labeling_0.4.3 compiler_4.3.0
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。