R語言ggplot2做漂亮的抖動(dòng)散點(diǎn)圖的一個(gè)實(shí)例

在網(wǎng)上偶然間發(fā)現(xiàn)的一個(gè)R語言ggplot2做數(shù)據(jù)可視化的實(shí)例,提供數(shù)據(jù)和代碼混蔼,今天的推文把代碼拆解一下

實(shí)例數(shù)據(jù)下載鏈接

https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data?select=GlobalLandTemperaturesByCountry.csv

下載這個(gè)數(shù)據(jù)需要注冊kaggle

代碼鏈接

https://github.com/cnicault/30DayChartChallenge/blob/main/day12/day12_strips.Rmd

結(jié)果圖

image.png

這個(gè)圖展示的是法國1980年前后的溫度差異零聚,數(shù)據(jù)里提供很多個(gè)國家的數(shù)據(jù),可以自己更改成其他國家的數(shù)據(jù)試試

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

這里接觸了兩個(gè)新的R包

  • vroom
  • here
climate <- vroom::vroom(here::here("GlobalLandTemperaturesByCountry.csv"))

關(guān)于lubridate包中的函數(shù)的一些用法

lubridate::year("1743-11-01")
lubridate::month("1743-11-01")
lubridate::month("1743-11-01",label = T)
lubridate::month("1743-11-01",label = F)
lubridate::day("1743-11-01")

構(gòu)建作圖的數(shù)據(jù)集

library(tidyverse)
monthly <- climate %>%
  filter(Country == "France", !is.na(AverageTemperature)) %>%
  mutate(year = lubridate::year(dt),
         month = lubridate::month(dt, label = TRUE),
         pos = lubridate::month(dt, label = FALSE),
         color = ifelse(year > 1980, "Recent", "Past")) %>%
  filter(year >=1900) 

他這里先做了一個(gè)空白的熱圖

注釋里寫的是為了得到一個(gè)矩形的圖例

library(ggplot2)
ggplot() +
  # empty tile to get a legend with rectangle key
  geom_tile(data = monthly, 
            aes(x = 0, y =0, width =0, 
                height = 0, fill = color))
image.png

接下來是添加線段

seg <- tibble(x = c(0, 0, 10, 0, 9, 3, 8, 5, 6),
              xend = c(12.5, 3, 12.5, 5, 12.5, 6, 11, 10, 8),
              y = c(0, 5, 5, 10, 10, 15, 15, 20, 25),
              yend = c(0, 5, 5, 10, 10, 15, 15, 20, 25))

ggplot() +
  # empty tile to get a legend with rectangle key
  geom_tile(data = monthly, 
            aes(x = 0, y =0, 
                width =0, 
                height = 0, 
                fill = color)) +
  # y-axis
  geom_segment(data = seg, 
               aes(x = x, xend = xend,
                   y = y, yend = yend), 
               color = "red", 
               linetype = "12") 
image.png

添加文本注釋

seg_lab <- tibble(x = c(0, 0, 0, 3, 5, 6),
                  y = seq(0,25, 5))
ggplot() +
  # empty tile to get a legend with rectangle key
  geom_tile(data = monthly, 
            aes(x = 0, y =0, 
                width =0, 
                height = 0, 
                fill = color)) +
  # y-axis
  geom_segment(data = seg, 
               aes(x = x, xend = xend,
                   y = y, yend = yend), 
               color = "black", linetype = "12") +
  geom_text(data = seg_lab, aes(x = x, y = y, 
                                label = glue::glue("{y} °C")), 
            color = "black", nudge_y = 1, 
            family = "serif", hjust = 0) 
image.png

添加抖動(dòng)的散點(diǎn)

ggplot() +
  # empty tile to get a legend with rectangle key
  geom_tile(data = monthly, 
            aes(x = 0, y =0, 
                width =0, 
                height = 0, 
                fill = color)) +
  # y-axis
  geom_segment(data = seg, 
               aes(x = x, xend = xend, 
                   y = y, yend = yend), 
               color = "white", 
               linetype = "12") +
  geom_text(data = seg_lab, 
            aes(x = x, y = y, 
                label = glue::glue("{y} °C")), 
            color = "white", nudge_y = 1, 
            family = "serif", hjust = 0) +
  # show.legend = FALSE to remove the shape of the point in the legend
  geom_jitter(data = filter(monthly, color == "Recent"), 
              aes(x = pos+0.2, y = AverageTemperature, 
                  fill = color), width = 0.15,
              height =0, size = 3, shape = 21, 
              stroke = 0.3, color = "#FFDADC", 
              show.legend = FALSE) +
  geom_jitter(data = filter(monthly, color == "Past"), 
              aes(x = pos-0.2, y = AverageTemperature, 
                  fill = color), width = 0.15,
              height =0, size = 2.5, shape = 21, 
              stroke = 0.3, color = "#93E2F5", 
              show.legend = FALSE) 
image.png

接下來就是對細(xì)節(jié)的調(diào)整了

axis_labels <- tibble(month = lubridate::month(seq(1,12,1), 
                                               label = TRUE),
                      pos = seq(1,12,1))



txt_clr <- "white"
pal1 <- c("#105182", "#1a7bc5", "#42a2f1", "#E9F1F2", "#ff9193", "#f1434a", "#c91022", "#8d0613", "#4D030A")



monthly_plt <- ggplot() +
  # empty tile to get a legend with rectangle key
  geom_tile(data = monthly, 
            aes(x = 0, y =0, 
                width =0, height = 0, 
                fill = color)) +
  # y-axis
  geom_segment(data = seg, 
               aes(x = x, xend = xend, 
                   y = y, yend = yend), 
               color = "white", linetype = "12") +
  geom_text(data = seg_lab, 
            aes(x = x, y = y, label = glue::glue("{y} °C")), 
            color = "white", nudge_y = 1, 
            family = "serif", hjust = 0) +
  # show.legend = FALSE to remove the shape of the point in the legend
  geom_jitter(data = filter(monthly, color == "Recent"), 
              aes(x = pos+0.2, y = AverageTemperature, fill = color), 
              width = 0.15, height =0, size = 3, 
              shape = 21, stroke = 0.3, color = "#FFDADC", show.legend = FALSE) +
  geom_jitter(data = filter(monthly, color == "Past"), 
              aes(x = pos-0.2, y = AverageTemperature, fill = color), 
              width = 0.15, height =0, size = 2.5, 
              shape = 21, stroke = 0.3, color = "#93E2F5", 
              show.legend = FALSE) +
  # x-axis labels
  geom_text(data = axis_labels, 
            aes(x = pos, y = -2, label = month), 
            color = "white", vjust = 0, 
            angle = 90, size = 5, family = "serif")+
  # scales
  scale_fill_manual(values = c("Recent" = "#f1434a", "Past" = "#1a7bc5"), 
                    labels = c("Recent" = "> 1980", "Past" = "<= 1980")) +
  scale_y_continuous(limits = c(-4,26), 
                     breaks = seq(0,25,5)) +
  labs(fill = "Observations") +
  theme_void() +
  guides(fill = guide_legend(label.position = "top",
                             title.hjust = 0.5,
                             keyheight = unit(1, "line"),
                             keywidth = unit(4, "line"),
                             nrow = 1),
         color = FALSE) +
  theme(plot.background = element_rect(fill = "grey40", color = NA),
        legend.position = c(0.13, 0.85),
        legend.text = element_text(face = "bold", 
                                   size = 12, color = txt_clr),
        legend.title = element_text(face = "bold", size = 14, color = txt_clr))

monthly_plt
image.png

歡迎大家關(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)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市蜒程,隨后出現(xiàn)的幾起案子绅你,更是在濱河造成了極大的恐慌伺帘,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件忌锯,死亡現(xiàn)場離奇詭異伪嫁,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)偶垮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門张咳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人似舵,你說我怎么就攤上這事脚猾。” “怎么了砚哗?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵龙助,是天一觀的道長。 經(jīng)常有香客問我蛛芥,道長提鸟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任仅淑,我火速辦了婚禮称勋,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘涯竟。我一直安慰自己赡鲜,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布庐船。 她就那樣靜靜地躺著银酬,像睡著了一般。 火紅的嫁衣襯著肌膚如雪筐钟。 梳的紋絲不亂的頭發(fā)上捡硅,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天,我揣著相機(jī)與錄音盗棵,去河邊找鬼壮韭。 笑死,一個(gè)胖子當(dāng)著我的面吹牛纹因,可吹牛的內(nèi)容都是我干的喷屋。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼瞭恰,長吁一口氣:“原來是場噩夢啊……” “哼屯曹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤恶耽,失蹤者是張志新(化名)和其女友劉穎密任,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體偷俭,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡浪讳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涌萤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片淹遵。...
    茶點(diǎn)故事閱讀 39,841評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖负溪,靈堂內(nèi)的尸體忽然破棺而出透揣,到底是詐尸還是另有隱情,我是刑警寧澤川抡,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布辐真,位于F島的核電站,受9級特大地震影響崖堤,放射性物質(zhì)發(fā)生泄漏拆祈。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一倘感、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咙咽,春花似錦老玛、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至溉苛,卻和暖如春镜廉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背愚战。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工娇唯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人寂玲。 一個(gè)月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓塔插,卻偏偏與公主長得像,于是被迫代替她去往敵國和親拓哟。 傳聞我的和親對象是個(gè)殘疾皇子想许,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評論 2 354

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