跟着Nature Genetics 学画图:R语言ggplot2画世界地图的简单小例子

论文是 Whole-genome resequencing of 445 Lactuca accessions reveals the domestication history of cultivated lettuce

image.png

这篇论文的数据是公开的,我们可以试着用公开的数据复现一下论文中用来展示数据的图。第一个图是使用地图来展示实验样本的地理分布。论文中写道 画图是使用ggplot2,作图数据来自 the Natural Earth dataset (http://www.naturalearthdata.com).

The world map was constructed using the R package ggplot2 with the Natural Earth dataset.

(http://www.naturalearthdata.com) 这个链接的数据怎么下载暂时没有搞明白。查了一下,发现R语言里有专门的包来获取这个地图数据,参考链接是 https://slcladal.github.io/maps.html

先安装获取地图数据的两个包
install.packages("rnaturalearth")
install.packages("rnaturalearthdata")

这个地方还需要安装rgeos这个包,要不然会遇到报错

Error in st_as_sfc.SpatialPolygons(sp::geometry(x), ...) : 
  package rgeos required for finding out which hole belongs to which exterior ring
image.png

这个地方我试着用install.packages("rgeos")来安装没有成功

image.png

然后就直接下载了包的源码进行本地安装,成功

下载链接

https://cran.r-project.org/web/packages/rgeos/index.html

image.png
image.png
画地图
library(ggplot2)
# gene world map
ggplot(data = world) +
  geom_sf() +
  labs( x = "Longitude", y = "Latitude") +
  ggtitle("World map",
          subtitle = paste0("(", 
                            length(unique(world$admin)),
                            " countries)"))
image.png

那么应该如何给海洋的部分填充蓝色呢?上面提到的链接里是直接把背景改成蓝色就好了

library(ggplot2)
# gene world map
ggplot(data = world) +
  geom_sf(fill="white") +
  labs( x = "Longitude", y = "Latitude") +
  # ggtitle("World map",
  #         subtitle = paste0("(", 
  #                           length(unique(world$admin)),
  #                           " countries)"))+
  theme_bw()+
  theme(#plot.background = element_rect(fill = "aliceblue"),
        panel.grid = element_blank(),
        panel.background = element_rect(fill = "aliceblue"))+
  labs(x=NULL,y=NULL)
image.png

这个图对应的是论文中fig1a中右上角的完整地图,但是还没有添加表示取样地点的点和表示样本比例的饼图

image.png
  • 接下来就是如何添加表示比例的饼图了
  • 如何只显示部分区域呢?

这两个问题有时间再来研究吧!

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

  • https://www.datanovia.com/en/blog/how-to-create-a-map-using-ggplot2/
    这个链接也是介绍画地图的

你可能感兴趣的:(跟着Nature Genetics 学画图:R语言ggplot2画世界地图的简单小例子)