跟著Nature Genetics 學(xué)畫圖:R語言ggplot2畫世界地圖的簡單小例子

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

image.png

這篇論文的數(shù)據(jù)是公開的,我們可以試著用公開的數(shù)據(jù)復(fù)現(xiàn)一下論文中用來展示數(shù)據(jù)的圖。第一個(gè)圖是使用地圖來展示實(shí)驗(yàn)樣本的地理分布。論文中寫道 畫圖是使用ggplot2,作圖數(shù)據(jù)來自 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) 這個(gè)鏈接的數(shù)據(jù)怎么下載暫時(shí)沒有搞明白。查了一下,發(fā)現(xiàn)R語言里有專門的包來獲取這個(gè)地圖數(shù)據(jù),參考鏈接是 https://slcladal.github.io/maps.html

先安裝獲取地圖數(shù)據(jù)的兩個(gè)包
install.packages("rnaturalearth")
install.packages("rnaturalearthdata")

這個(gè)地方還需要安裝rgeos這個(gè)包,要不然會(huì)遇到報(bào)錯(cuò)

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

這個(gè)地方我試著用install.packages("rgeos")來安裝沒有成功

image.png

然后就直接下載了包的源碼進(jìn)行本地安裝,成功

下載鏈接

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

那么應(yīng)該如何給海洋的部分填充藍(lán)色呢?上面提到的鏈接里是直接把背景改成藍(lán)色就好了

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

這個(gè)圖對應(yīng)的是論文中fig1a中右上角的完整地圖,但是還沒有添加表示取樣地點(diǎn)的點(diǎn)和表示樣本比例的餅圖

image.png
  • 接下來就是如何添加表示比例的餅圖了
  • 如何只顯示部分區(qū)域呢?

這兩個(gè)問題有時(shí)間再來研究吧!

歡迎大家關(guān)注我的公眾號(hào)

小明的數(shù)據(jù)分析筆記本

小明的數(shù)據(jù)分析筆記本 公眾號(hào) 主要分享:1、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)、基因組學(xué)、群體遺傳學(xué)文獻(xiàn)閱讀筆記;3、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容