<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          R語(yǔ)言可視化學(xué)習(xí)筆記之ggridges包繪制山巒圖

          共 1373字,需瀏覽 3分鐘

           ·

          2020-09-26 12:01

          作者:嚴(yán)濤 浙江大學(xué)作物遺傳育種在讀研究生(生物信息學(xué)方向)偽碼農(nóng),R語(yǔ)言愛(ài)好者,愛(ài)開(kāi)源。

          嚴(yán)濤老師的繪圖教程還有:

          gganimate |諾獎(jiǎng)文章里面的動(dòng)圖繪制教程來(lái)了!!

          ggplot2學(xué)習(xí)筆記之圖形排列

          R包ggseqlogo |繪制序列分析圖

          ggplot2高效實(shí)用指南 (可視化腳本、工具、套路、配色)

          簡(jiǎn)介

          ggridges包主要用來(lái)繪制山巒圖。尤其是針對(duì)時(shí)間或者空間分布可視化具有十分好的效果。ggridges主要提供兩個(gè)幾何圖像函數(shù):

          • geom_ridgeline(): 主要繪制山脊線(xiàn)圖

          • geom_density_ridges():主要繪制密度山脊線(xiàn)圖

          具體用法可以參考官方文檔:

          https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html

          geom_ridgeline()

          library(ggridges)
          library(tidyverse)

          # grid.arrange來(lái)自于gridExtra包,可以同時(shí)拼圖多個(gè)ggplot2對(duì)象
          library(gridExtra)


          my_data <- data.frame(x=1:5, y=rep(1,5), height=c(0,1,-1,3,2))
          plot_base <- ggplot(my_data, aes(x, y, height=height))


          # 默認(rèn)負(fù)值不顯示,除非指定min_height參數(shù)

          grid.arrange(plot_base+geom_ridgeline(),
          ? ? ? ? ? ? plot_base+geom_ridgeline(min_height=-2), ncol=2)

          geom_density_ridges()

          geom_density_ridges()函數(shù)首先會(huì)根據(jù)數(shù)據(jù)計(jì)算密度然后繪圖,此時(shí)美學(xué)映射height沒(méi)有必要寫(xiě)入函數(shù)中。下面使用lincoln_weather數(shù)據(jù)集。

          # creates a vector of n equally spaced colors along the
          # Matplolib 'viridis' color map

          # also designed to be perceived by readers with the most common form of color blindness
          # ?scale_fill_viridis函數(shù)來(lái)源于此包,
          # 其參數(shù) option用于設(shè)置顏色 "magma" (or "A"), "inferno" (or "B"), "plasma" (or "C"),
          and "viridis" (or "D", the default option).
          # ?viridis可以查看其具體含義

          library(viridis)
          head(lincoln_weather[ ,1:4])


          ## # A tibble: 6 x 4
          ## ? CST ? ? ?`Max Temperature [F]` `Mean Temperature [F]` `Min Temperature ~
          ## ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
          ## 1 2016-1-1 ? ? ? ? ? ? ? ? ? ?37 ? ? ? ? ? ? ? ? ? ? 24 ? ? ? ? ? ? ? ? 11
          ## 2 2016-1-2 ? ? ? ? ? ? ? ? ? ?41 ? ? ? ? ? ? ? ? ? ? 23 ? ? ? ? ? ? ? ? ?5
          ## 3 2016-1-3 ? ? ? ? ? ? ? ? ? ?37 ? ? ? ? ? ? ? ? ? ? 23 ? ? ? ? ? ? ? ? ?8
          ## 4 2016-1-4 ? ? ? ? ? ? ? ? ? ?30 ? ? ? ? ? ? ? ? ? ? 17 ? ? ? ? ? ? ? ? ?4
          ## 5 2016-1-5 ? ? ? ? ? ? ? ? ? ?38 ? ? ? ? ? ? ? ? ? ? 29 ? ? ? ? ? ? ? ? 19
          ## 6 2016-1-6 ? ? ? ? ? ? ? ? ? ?34 ? ? ? ? ? ? ? ? ? ? 33 ? ? ? ? ? ? ? ? 32

          # x后的值用 ` (反引號(hào))括起,是因?yàn)榱忻种写嬖诳崭窈吞厥庾址枰厥鈱?duì)待
          # fill = ..x.., double dots是ggplot2的一種特殊識(shí)別符,用來(lái)區(qū)分定義的和計(jì)算的美學(xué)參數(shù)
          # 這里指用橫軸的數(shù)據(jù)著色

          ggplot(lincoln_weather, aes(x=`Mean Temperature [F]`, y=`Month`, fill=..x..))+
          ?geom_density_ridges_gradient(scale=3, rel_min_height=0.01, gradient_lwd = 1.)+
          ?scale_x_continuous(expand = c(0.01, 0))+ # 擴(kuò)展下橫軸和縱軸
          ?scale_y_discrete(expand = c(0.01,0))+
          ?scale_fill_viridis(name="Temp. [F]", option = "C")+
          ?labs(title="Temperature in Lincoln NE",
          ? ? ? subtitle="Mean temperature (Fahrenheit) by month for 2016\nData:Orogin CSV from the Weather Underground ")+
          ?theme_ridges(font_size = 13, grid = FALSE)+
          ?theme(axis.title.y = element_blank())

          cyclinal scales

          為了使得ggridges繪制的圖形可視化效果最好,同時(shí)為了減少用戶(hù)對(duì)顏色設(shè)置的困難,作者提供了cyclinal scales用于顏色輪轉(zhuǎn)映射。

          ggplot(diamonds, aes(x=price, y=cut, fill=cut))+
          ?geom_density_ridges(scale=4)+
          ?scale_fill_cyclical(values = c("blue", "green"))+
          ?theme_ridges(grid = FALSE)

          默認(rèn)的,cyclinal scales為了防止誤解是不繪制圖例的,但是可以通過(guò)選項(xiàng)guide="legend"添加圖例。

          ggplot(diamonds, aes(x=price, y=cut, fill=cut))+
          ?geom_density_ridges(scale=4)+
          ?scale_fill_cyclical(values = c("blue", "green"), guide="legend")+
          ?theme_ridges(grid = FALSE)


          ggplot2一樣,圖例是可以修改的,其他參數(shù)比如大小、透明度、形狀等都是可以通過(guò)cyclinal scales修改。

          ggplot(diamonds, aes(x=price, y=cut, fill=cut))+
          ?geom_density_ridges(scale=4)+
          ?scale_fill_cyclical(values = c("blue", "green"), guide="legend",
          ? ? ? ? ? ? ? ? ? ? ?labels=c("Fair"="blue", "Good"="green"),
          ? ? ? ? ? ? ? ? ? ? ?name="Fill colors")+
          ?theme_ridges(grid = FALSE)

          再來(lái)2個(gè)示例

          不做解釋了,如果想重現(xiàn)就把代碼拆解開(kāi),按需修改。一句句話(huà)單獨(dú)拆開(kāi)運(yùn)行,理解其操作內(nèi)容。

          library(dplyr)
          library(forcats)
          Catalan_elections %>%
          ?mutate(YearFct = fct_rev(as.factor(Year))) %>%
          ?ggplot(aes(y = YearFct)) +
          ?geom_density_ridges(
          ? ?aes(x = Percent, fill = paste(YearFct, Option)),
          ? ?alpha = .8, color = "white", from = 0, to = 100
          ?) +
          ?labs(
          ? ?x = "Vote (%)",
          ? ?y = "Election Year",
          ? ?title = "Indy vs Unionist vote in Catalan elections",
          ? ?subtitle = "Analysis unit: municipalities (n = 949)",
          ? ?caption = "Marc Belzunces (@marcbeldata) | Source: Idescat"
          ?) +
          ?scale_y_discrete(expand = c(0.01, 0)) +
          ?scale_x_continuous(expand = c(0.01, 0)) +
          ?scale_fill_cyclical(
          ? ?breaks = c("1980 Indy", "1980 Unionist"),
          ? ?labels = c(`1980 Indy` = "Indy", `1980 Unionist` = "Unionist"),
          ? ?values = c("#ff0000", "#0000ff", "#ff8080", "#8080ff"),
          ? ?name = "Option", guide = "legend"
          ?) +
          ?theme_ridges(grid = FALSE)

          library(DAAG) # for ais dataset
          ais$sport <- factor(
          ?ais$sport,
          ?levels = c("B_Ball", "Field", "Gym", "Netball", "Row", "Swim", "T_400m", "T_Sprnt", "Tennis", "W_Polo"),
          ?labels = c("Basketball", "Field", "Gym", "Netball", "Row", "Swim", "Track 400m", "Track Sprint", "Tennis", "Water Polo")
          )

          ggplot(ais, aes(x=ht, y=sport, color=sex, point_color=sex, fill=sex)) +
          ?geom_density_ridges(
          ? ?jittered_points=TRUE, scale = .95, rel_min_height = .01,
          ? ?point_shape = "|", point_size = 3, size = 0.25,
          ? ?position = position_points_jitter(height = 0)
          ?) +
          ?scale_y_discrete(expand = c(.01, 0)) +
          ?scale_x_continuous(expand = c(0, 0), name = "height [cm]") +
          ?scale_fill_manual(values = c("#D55E0050", "#0072B250"), labels = c("female", "male")) +
          ?scale_color_manual(values = c("#D55E00", "#0072B2"), guide = "none") +
          ?scale_discrete_manual("point_color", values = c("#D55E00", "#0072B2"), guide = "none") +
          ?guides(fill = guide_legend(
          ? ?override.aes = list(
          ? ? ?fill = c("#D55E00A0", "#0072B2A0"),
          ? ? ?color = NA, point_color = NA))
          ?) +
          ?ggtitle("Height in Australian athletes") +
          ?theme_ridges(center = TRUE)

          還有很多用法有興趣的可以查看官方文檔https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html和https://cran.r-project.org/web/packages/ggridges/vignettes/gallery.html)繼續(xù)學(xué)習(xí)。

          R統(tǒng)計(jì)和作圖

          高顏值免費(fèi)在線(xiàn)繪圖




          往期精品

          畫(huà)圖三字經(jīng)?生信視頻?生信系列教程?

          心得體會(huì)?TCGA數(shù)據(jù)庫(kù)?Linux?Python?

          高通量分析?免費(fèi)在線(xiàn)畫(huà)圖?測(cè)序歷史?超級(jí)增強(qiáng)子

          生信學(xué)習(xí)視頻?PPT?EXCEL?文章寫(xiě)作?ggplot2

          海哥組學(xué)?可視化套路?基因組瀏覽器

          色彩搭配?圖形排版?互作網(wǎng)絡(luò)

          自學(xué)生信?2019影響因子?GSEA?單細(xì)胞?

          后臺(tái)回復(fù)“生信寶典福利第一波獲取教程合集




          瀏覽 165
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国语对白久久 | 国产又爽 又黄 免费网站在线 | 超碰在线观看中文字幕版 | 黄色大全在线观看 | 欧美日韩a级大全免费高清在线播放 |