R语言~绘制地图

用于绘制轻量级样本分布图,提升文章逼格

加载程序包

library(ggpubr)
library(rgdal)
library(plotly)
library(ggsci)
library(BIOMEplot)# install_github("kunstler/BIOMEplot")
library(devtools)
library(RColorBrewer)

读取样本地理坐标

bac_sites = read.delim('../Global_Tomb/Result/Bacteria/SILVA138/Filter_Outliers/00_load_data/USED_Bacteria_map.txt', row.names = 1, header = T, sep = '\t')
fungi_sites = read.delim('../Global_Tomb/Result/Fungi/00_closed/00_load_data/USED_Fungi_map.txt',row.names = 1, header = T, sep = '\t')

sites = rbind(bac_sites[c('Latitude','Longitude', 'Category', 'Site')],fungi_sites[c('Latitude','Longitude', 'Category', 'Site')])
sites = unique.data.frame(sites)
names(sites) <- c('lat','lon', 'category' ,'name')

读取地图图层

图层文件下载自http//www.naturalearthdata.com/download/110m/physical/ne_110m_land.zip

#世界地图 
wmap <- readOGR(dsn = "maps/ne_110m_land/ne_110m_land.shp", layer="ne_110m_land")
#国家地图或国界线
countries <- readOGR("maps/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp", layer="ne_110m_admin_0_countries")
  
 #地图图层投影到WGS84坐标系
wmap_proj <- spTransform(wmap, CRS("+proj=longlat +datum=WGS84"))
countries_proj <- spTransform(countries, CRS("+proj=longlat +datum=WGS84"))

 #将样本坐标投影至WGS84坐标系
coordinates(sites) <- c("lon","lat")
proj4string(sites) <- CRS("+proj=longlat +datum=WGS84")
sites_proj <- spTransform(sites, CRS("+proj=longlat +datum=WGS84"))

#转换投影对象至ggplot数据格式
wmap_proj_df <- fortify(wmap_proj)
countries_proj_df <- fortify(countries_proj)
sites_proj_df <- data.frame(sites_proj)

  
sites_proj_df$category <- as.factor(sites_proj_df$category)

  p2 <- ggplot() +
    geom_polygon(data=wmap_proj_df, aes(long,lat,group=group), col = "grey", fill="white", size = 0.2) +
    geom_path(data=countries_proj_df, aes(long,lat, group=group), color="grey",size=0.2) +
    geom_point(data=sites_proj_df, aes(lon, lat, col = category), size = 0.5) + 
    # scale_color_igv() +
    guides(color = guide_legend(override.aes = list(size = 2))) +
    scale_color_d3() +
    # scale_y_continuous(expand = c(0,0)) +
    # scale_x_continuous(expand = c(0,0)) +
    scale_size(range=c(1,7), guide = "legend") + theme_bw() +  # Remove ugly grey background
    labs(x = NULL, y = NULL, col = NULL) + 
    theme_void() +
    theme(axis.title = element_blank(),
          axis.text = element_blank(),
          panel.grid = element_blank(),
          plot.background = element_rect(fill = 'grey'),
          legend.key.height = unit(0.2,'cm'),
          legend.spacing.x = unit(-0.2, 'cm'),
          legend.text = element_text(color = 'black', size = 8),
          legend.position = c(0.1,0.25),
          legend.background = element_blank(),
          legend.box.background = element_blank())
Sites.jpg

你可能感兴趣的:(R语言~绘制地图)