R語言ggplot2畫漂亮的環(huán)形柱形圖的一個實(shí)例

在twitter上看到一個圖

image.png

配色很漂亮,代碼和數(shù)據(jù)也是公開的殴俱,今天的推文來學(xué)習(xí)一下他的代碼

代碼來源的鏈接是 https://github.com/NearAndDistant/data_science_with_r

這個鏈接還有很多其他的R語言ggplot2作圖的例子敛摘,代碼和數(shù)據(jù)都是公開的削茁,大家自己有時間可以重復(fù)一下其中的代碼

image.png

這個環(huán)形柱形圖的代碼是以shiny app的形式提供的聪建,這里我們忽略shiny app,只把作圖代碼拆解出來

首先是整理數(shù)據(jù)的代碼

library(tidyverse)
# import data for project
breed_traits_raw      <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_traits.csv')
breed_rank_all_raw    <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-02-01/breed_rank.csv')
### Clean and Wrangle 
# dogs rank clean
dogs_rank_long <- 
  breed_rank_all_raw %>%
  pivot_longer(cols = c(`2013 Rank`:`2020 Rank`), names_to = "year", values_to = "rank") %>%
  mutate(year = as.numeric(str_remove(year, " Rank"))) %>%
  select(Breed, year, rank, everything()) %>%
  janitor::clean_names() %>%
  mutate(breed = str_squish(breed))
# dog traits clean
dogs_trait_long <-
  breed_traits_raw %>%
  select(-`Coat Type`, -`Coat Length`) %>%
  pivot_longer(cols = c(`Affectionate With Family` : `Mental Stimulation Needs`), names_to = "attribute", values_to = "value") %>%
  janitor::clean_names() %>%
  mutate(breed = str_squish(breed))
# transform
top_dogs <-
dogs_rank_long %>%
  left_join(dogs_trait_long) %>%
  filter(year == 2020) %>%
  mutate(breed = as_factor(breed)) %>%
  group_by(attribute) %>%
    mutate(attribute = str_remove(attribute, " Level"),
           attribute = case_when(attribute == "Affectionate With Family"   ~ "Affectionate",
                                 attribute == "Good With Young Children"   ~ "Child-Friendly",
                                 attribute == "Good With Other Dogs"       ~ "Combativeness",
                                 attribute == "Openness To Strangers"      ~ "Openness",
                                 attribute == "Watchdog/Protective Nature" ~ "Protective",
                                 attribute == "Coat Grooming Frequency"    ~ "Grooming",
                                 attribute == "Mental Stimulation Needs"   ~ "Stimulation",
                                 TRUE ~ attribute)) %>%
    mutate(attribute = factor(attribute)) %>%
  ungroup() %>%
  group_by(breed) %>%
    arrange(desc(value)) %>%
    mutate(id = row_number()) %>%
  ungroup() %>% #2 Pissaro #1 Signac
  mutate(fill = case_when(attribute == "Affectionate"   ~  "#fbe183",
                          attribute == "Child-Friendly" ~  "#2b9b81",
                          attribute == "Combativeness"  ~  "#d8443c",
                          attribute == "Openness"       ~  "#e6a2a6",
                          attribute == "Playfulness"    ~  "#9f5691",
                          attribute == "Adaptability"   ~  "#f4c40f",
                          attribute == "Trainability"   ~  "#aa7aa1",
                          attribute == "Energy"         ~  "#fe9b00",
                          attribute == "Protective"     ~  "#e87b89",
                          attribute == "Stimulation"    ~  "#de597c",
                          attribute == "Barking"        ~  "#9b3441",
                          attribute == "Grooming"       ~  "#92c051",
                          attribute == "Shedding"       ~  "#633372",
                          attribute == "Drooling"       ~  "#1f6e9c"))

這部分我們就不介紹了,運(yùn)行完上述代碼可以拿到top_dogs這個數(shù)據(jù)集

如果讀取數(shù)據(jù)的部分不能訪問氧腰,我把數(shù)據(jù)集下載下來了枫浙,可以在公眾號后臺留言20220210獲取

接下來作圖是從top_dogs這個數(shù)據(jù)集開始

首先是讀取數(shù)據(jù)

top_dogs<-read.csv("top_dogs.csv")
head(top_dogs)

畫圖代碼

首先是背景的圈和文字

top_dogs %>% 
  filter(breed == "Russell Terriers") %>% 
  ggplot() +
  geom_segment(data = data.frame(y=seq(0,5,1)), 
               aes(x = -0.5, xend = 15, y=y, yend=y), 
               linetype = "ff", color = "grey90") +
  geom_text(data = data.frame(y=seq(0,5,1)), 
            aes(x = -0.15 , y = y + 0.5, label = y), 
            family = "serif", 
            size = 3, fontface = "bold") +
  coord_polar(clip = "off") +
  geom_text(aes(x = id, y = 7, label = attribute), 
            size = 3, fontface = 'bold', 
            family = "serif") +
  geom_text(aes(label = breed),
            x = -0.5, y = -1.7, size = 4, 
            fontface = 'bold', 
            family = "serif") 

然后是添加柱子

top_dogs %>% 
  filter(breed == "Russell Terriers") %>% 
  ggplot() +
  geom_segment(data = data.frame(y=seq(0,5,1)), 
               aes(x = -0.5, xend = 15, y=y, yend=y), 
               linetype = "ff", color = "grey90") +
  geom_text(data = data.frame(y=seq(0,5,1)), 
            aes(x = -0.15 , y = y + 0.5, label = y), 
            family = "serif", 
            size = 3, fontface = "bold") +
  coord_polar(clip = "off") +
  geom_text(aes(x = id, y = 7, label = attribute), 
            size = 3, fontface = 'bold', 
            family = "serif") +
  geom_text(aes(label = breed),
            x = -0.5, y = -1.7, size = 4, 
            fontface = 'bold', 
            family = "serif") +
  geom_col(aes(id, value, fill = fill), 
           show.legend = FALSE) 
image.png

設(shè)置內(nèi)部空心化

top_dogs %>% 
  filter(breed == "Russell Terriers") %>% 
  ggplot() +
  geom_segment(data = data.frame(y=seq(0,5,1)), 
               aes(x = -0.5, xend = 15, y=y, yend=y), 
               linetype = "ff", color = "grey90") +
  geom_text(data = data.frame(y=seq(0,5,1)), 
            aes(x = -0.15 , y = y + 0.5, label = y), 
            family = "serif", 
            size = 3, fontface = "bold") +
  coord_polar(clip = "off") +
  geom_text(aes(x = id, y = 7, label = attribute), 
            size = 3, fontface = 'bold', 
            family = "serif") +
  geom_text(aes(label = breed),
            x = -0.5, y = -1.7, size = 4, 
            fontface = 'bold', 
            family = "serif") +
  geom_col(aes(id, value, fill = fill), 
           show.legend = FALSE) +
  scale_fill_identity() +
  scale_y_continuous(limits = c(-5.5, 7), breaks = seq(0,5,1)) +
  scale_x_continuous(limits = c(-0.5, max(top_dogs$id)+1)) 
image.png

在內(nèi)部添加圖片

top_dogs %>% 
  filter(breed == "Russell Terriers") %>% 
  ggplot() +
  geom_segment(data = data.frame(y=seq(0,5,1)), 
               aes(x = -0.5, xend = 15, y=y, yend=y), 
               linetype = "ff", color = "grey90") +
  geom_text(data = data.frame(y=seq(0,5,1)), 
            aes(x = -0.15 , y = y + 0.5, label = y), 
            family = "serif", 
            size = 3, fontface = "bold") +
  coord_polar(clip = "off") +
  geom_text(aes(x = id, y = 7, label = attribute), 
            size = 3, fontface = 'bold', 
            family = "serif") +

  geom_col(aes(id, value, fill = fill), 
           show.legend = FALSE) +
  scale_fill_identity() +
  scale_y_continuous(limits = c(-5.5, 7), breaks = seq(0,5,1)) +
  scale_x_continuous(limits = c(-0.5, max(top_dogs$id)+1)) +
  ggimage::geom_image(aes(x = -0.5, y = -5.5, 
                          image = image), 
                      size = 0.24) +
  geom_text(aes(label = breed),
            x = -0.5, y = -1.7, size = 4, 
            fontface = 'bold', 
            family = "serif") +
  theme_void() +
  theme(plot.margin = margin(1.5,0,0,0, unit = "cm"))

這里需要注意的一點(diǎn)是 需要把添加狗的品種名的代碼放到添加圖片的代碼的后面,要不然會有遮蓋

image.png

同樣的代碼在話另外一個品種


image.png

最后來一個拼圖

library(patchwork)
p1+p2
image.png

示例數(shù)據(jù)和代碼可以在公眾號后臺留言20220210獲取

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

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

小明的數(shù)據(jù)分析筆記本 公眾號 主要分享: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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市挺尾,隨后出現(xiàn)的幾起案子鹅搪,更是在濱河造成了極大的恐慌,老刑警劉巖遭铺,帶你破解...
    沈念sama閱讀 206,126評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丽柿,死亡現(xiàn)場離奇詭異,居然都是意外死亡魂挂,警方通過查閱死者的電腦和手機(jī)甫题,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,254評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來涂召,“玉大人坠非,你說我怎么就攤上這事」” “怎么了炎码?”我有些...
    開封第一講書人閱讀 152,445評論 0 341
  • 文/不壞的土叔 我叫張陵盟迟,是天一觀的道長。 經(jīng)常有香客問我辅肾,道長队萤,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,185評論 1 278
  • 正文 為了忘掉前任矫钓,我火速辦了婚禮,結(jié)果婚禮上舍杜,老公的妹妹穿的比我還像新娘新娜。我一直安慰自己,他們只是感情好既绩,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,178評論 5 371
  • 文/花漫 我一把揭開白布概龄。 她就那樣靜靜地躺著,像睡著了一般饲握。 火紅的嫁衣襯著肌膚如雪私杜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 48,970評論 1 284
  • 那天救欧,我揣著相機(jī)與錄音衰粹,去河邊找鬼。 笑死笆怠,一個胖子當(dāng)著我的面吹牛铝耻,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蹬刷,決...
    沈念sama閱讀 38,276評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼瓢捉,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了办成?” 一聲冷哼從身側(cè)響起泡态,我...
    開封第一講書人閱讀 36,927評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎迂卢,沒想到半個月后某弦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,400評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冷守,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,883評論 2 323
  • 正文 我和宋清朗相戀三年刀崖,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拍摇。...
    茶點(diǎn)故事閱讀 37,997評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡亮钦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出充活,到底是詐尸還是另有隱情蜂莉,我是刑警寧澤蜡娶,帶...
    沈念sama閱讀 33,646評論 4 322
  • 正文 年R本政府宣布,位于F島的核電站映穗,受9級特大地震影響窖张,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蚁滋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,213評論 3 307
  • 文/蒙蒙 一宿接、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辕录,春花似錦睦霎、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,204評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蚣旱,卻和暖如春碑幅,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背塞绿。 一陣腳步聲響...
    開封第一講書人閱讀 31,423評論 1 260
  • 我被黑心中介騙來泰國打工沟涨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人位隶。 一個月前我還...
    沈念sama閱讀 45,423評論 2 352
  • 正文 我出身青樓拷窜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親涧黄。 傳聞我的和親對象是個殘疾皇子篮昧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,722評論 2 345

推薦閱讀更多精彩內(nèi)容