可视化R语言做图绘图技巧

初探circlize绘制弦图

2021-07-15  本文已影响0人  R语言数据分析指南

一直以来在介绍的数据可视化案例展示中,一直是围绕ggplot2而展开的,但是R中优秀的程序包除了ggplot2还有不少,如circlize,ggtree等,circlize在绘制弦图方面真是无敌的存在,因此本节就通过一个小例子来演示如何通过circlize绘制弦图,后期将陆续推出一系列相关教程,各位观众老爷敬请期待

加载R包

library(tidyverse)
library(ggtext)
library(circlize)

载入数据

netflix <- read_csv("netflix_titles.csv")

数据清洗

netflix_lgbtq <- netflix %>%
  filter(str_detect(listed_in,"LGBTQ Movies")) %>%
  separate(listed_in,into = c("genre1", "genre2", "genre3"),
           sep = ", ",fill = "right") %>%
  separate(date_added, into = c(NA, "added_year"), sep = ", ") %>%
  mutate(country = if_else(str_detect(title, "Wish You"),
                           "South Korea", country), 
         added_year = as.numeric(added_year)) %>%
  pivot_longer(genre1:genre3, names_to = "genre_num", values_to = "genre")

chord_lgbtq <- netflix_lgbtq %>% 
  filter(!is.na(genre) & genre != "LGBTQ Movies") %>%
  mutate(genre = factor(genre),
         added_year = as_factor(added_year)) %>% 
  group_by(genre) %>%
  mutate(count_genre = n()) %>%
  filter(count_genre > 1) %>% 
  ungroup() %>% 
  group_by(genre, added_year) %>% 
  count() %>% 
  ungroup()

自定义颜色

grid.col = c(`Comedies` = "#FF69B6",   
             `Cult Movies` = "#FF0018",
             `Documentaries` = "#FFA52C", 
             `Dramas` = "green", 
             `Independent Movies` = "#008018",
             `International Movies` = "#00C0C0", 
             `Music & Musicals` = "#400098", 
             `Romantic Movies` = "#86007D",
             `2015` = "green",`2016` = "green",
             `2017` = "green", `2018` = "green", 
             `2019` = "green", `2020` = "green",
             `2021` = "green")

数据可视化

circos.par(canvas.xlim=c(-1,1),canvas.ylim=c(-1,1),start.degree = 0)
chordDiagram(chord_lgbtq,
             order = c("Romantic Movies","Music & Musicals",
                       "International Movies", 
                       "Independent Movies", "Dramas",
                       "Documentaries", "Cult Movies",
                       "Comedies", "2015", "2016", "2017",
                       "2018", "2019", "2020", "2021"),
             link.sort = FALSE, 
             link.decreasing = TRUE, 
             grid.col = grid.col, 
             transparency = 0.1, 
             annotationTrack = "grid",
             preAllocateTracks = list(track.height = .1))

文本添加

for(si in get.all.sector.index()) {
  myCol <- grid.col[si]
  xlim = get.cell.meta.data("xlim",sector.index = si,track.index = 1)
  ylim = get.cell.meta.data("ylim",sector.index = si,track.index = 1)
  circos.text(mean(xlim), ylim[1],labels = si,sector.index = si,
              track.index = 1, 
              facing = "clockwise", 
              col = myCol,
              cex=0.8,
              adj=c(0,.5),
              niceFacing = T)
}

circos.clear()

喜欢的小伙伴欢迎关注我的公众号

R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助

上一篇 下一篇

猜你喜欢

热点阅读