[R 软件包分享] mapmixture 软件包优雅地绘制个性化地图
最编程
2024-07-14 14:13:05
...
欢迎关注R语言数据分析指南
❝本节来介绍一款新R包「mapmixture」其主要用来绘制地图并将数据以饼图的形式进行映射,非常的好用并且兼容ggplot2,更多详细内容请参考作者官方文档,有更多详细的案例。 ❞
官方文档
❝https://github.com/tom-jenkins/mapmixture ❞
加载R包
devtools::install_github("Tom-Jenkins/mapmixture")
library(tidyverse)
library(mapmixture)
library(terra)
library(gridExtra)
查看R包版本号
sessionInfo()
案例1
file <- system.file("extdata", "admixture1.csv", package = "mapmixture")
admixture1 <- read.csv(file)
file <- system.file("extdata", "coordinates.csv", package = "mapmixture")
coordinates <- read.csv(file)
map1 <- mapmixture(admixture1, coordinates, crs = 3035)
map1
案例2
file <- system.file("extdata", "admixture3.csv", package = "mapmixture")
admixture3 <- read.csv(file)
file <- system.file("extdata", "coordinates.csv", package = "mapmixture")
coordinates <- read.csv(file)
map2 <- mapmixture(admixture3, coordinates,
cluster_cols = c("green","blue"),
cluster_names = c("Group A","Group B"),
crs = 3035,
boundary = c(xmin=-15, xmax=16, ymin=40, ymax=62),
pie_size = 1,
pie_border = 0.3,
pie_opacity = 1,
land_colour = "#d9d9d9",
sea_colour = "#deebf7",
expand = TRUE,
arrow = TRUE,
arrow_size = 1.5,
arrow_position = "bl",
scalebar = TRUE,
scalebar_size = 1.5,
scalebar_position = "tl",
plot_title = "Admixture Map",
plot_title_size = 12,
axis_title_size = 10,
axis_text_size = 8)
map2
案例3
file <- system.file("extdata", "admixture1.csv", package = "mapmixture")
admixture1 <- read.csv(file)
file <- system.file("extdata", "coordinates.csv", package = "mapmixture")
coordinates <- read.csv(file)
map3 <- mapmixture(admixture1, coordinates,
cluster_cols = c("#f1a340","#998ec3"),
cluster_names = c("Ancestry 1","Ancestry 2"),
crs = 4326,
boundary = c(xmin=-15, xmax=16, ymin=40, ymax=62),
pie_size = 2,)+
annotate("label",x = -10,y = 46.5,label = "Atlantic Ocean",size = 3)+
annotate("text",x = 2.5,y = 57,label = "North Sea",size = 3)+
theme(
axis.title = element_text(size = 10),
axis.text = element_text(size = 8))+
guides(fill = guide_legend(override.aes = list(size = 5, alpha = 1)))
map3
案例4
file <- system.file("extdata", "admixture1.csv", package = "mapmixture")
admixture1 <- read.csv(file)
file <- system.file("extdata", "coordinates.csv", package = "mapmixture")
coordinates <- read.csv(file)
map4 <- mapmixture(admixture1, coordinates,
cluster_cols = c("#f1a340","#998ec3"),
cluster_names = c("Ancestry 1","Ancestry 2"),
crs = 4326,
boundary = c(xmin=-20, xmax=20, ymin=40, ymax=62),
pie_size = 2.5,
)+
theme(legend.position = "top",plot.margin = margin(l = 10, r = 10))+
guides(fill = guide_legend(override.aes = list(size = 5, alpha = 1)))
structure_barplot <- structure_plot(admixture1,
type = "structure",
cluster_cols = c("#f1a340","#998ec3"),
site_dividers = TRUE,
divider_width = 0.4,
site_order = c(
"Vigo","Ile de Re","Isles of Scilly","Mullet Peninsula",
"Shetland","Cromer","Helgoland","Flodevigen","Lysekil","Bergen"
),
labels = "site",
flip_axis = FALSE,
site_ticks_size = -0.05,
site_labels_y = -0.35,
site_labels_size = 2.2)+
theme(
axis.title.y = element_text(size = 8, hjust = 1),
axis.text.y = element_text(size = 5),
)
grid.arrange(map4, structure_barplot, nrow = 2, heights = c(4,1))
案例5
facet_barplot <- structure_plot(admixture1,
type = "facet",
cluster_cols = c("#f1a340","#998ec3"),
facet_col = 2,
ylabel = "Admixture proportions",
)+
theme(
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size = 5),
strip.text = element_text(size = 6, vjust = 1, margin = margin(t=1.5, r=0, b=1.5, l=0)),
)
grid.arrange(map4, facet_barplot, ncol = 2, widths = c(3,2))
案例6
「tiff文件下载网址」 https://www.naturalearthdata.com/downloads/50m-raster-data/50m-natural-earth-1/
earth <- terra::rast("NE1_50M_SR_W/NE1_50M_SR_W.tif")
file <- system.file("extdata", "admixture1.csv", package = "mapmixture")
admixture1 <- read.csv(file)
file <- system.file("extdata", "coordinates.csv", package = "mapmixture")
coordinates <- read.csv(file)
map6 <- mapmixture(admixture1, coordinates, crs = 3035, basemap = earth)
map6
下一篇: UNET