温度与山岭图

温度与山岭图

本文是学习Short and sweet: temperature variability with ggridges 📦的笔记,讲述了如何使用ggridges包绘制山岭图。

整理数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 获取数据
library(tidyverse)
library(ggridges)
library(awtools)

# O'Hare
ohare15 <- read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2015.csv') %>% select(-c(5))
ohare16 <- read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2016.csv') %>% select(-c(5))
ohare17 <- read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2017.csv') %>% select(-c(5))
ohare18 <- read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/OHARE-NCDC-2018.csv')

ohare <- ohare15 %>%
union(., ohare16) %>%
union(., ohare17) %>%
union(., ohare18) %>%
mutate(year = format(DATE, '%Y'),
month = factor(months(DATE),
levels = rev(c(
'一月', '二月', '三月',
'四月', '五月', '六月',
'七月', '八月', '九月',
'十月', '十一月', '十二月'
))),
TAVG = ifelse(is.na(TAVG), (TMAX + TMIN)/2, TAVG))

绘制O’ Hare的温度分布图:

1
2
3
4
5
6
7
8
9
10
11
ggplot(ohare, aes(x = TAVG, 
y = month,
fill = year)) +
geom_density_ridges(scale = 0.9,
color = NA) +
hrbrthemes::theme_ipsum(base_family = 'STSong') +
scale_fill_brewer('年份', palette = 'Set1') +
labs(title = 'O\'Hare年平均温度',
caption = '数据来源:NCDC\nhttps://www.ncdc.noaa.gov/',
x = '温度(F)',
y = '月份')

绘制LAX的温度分布图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
lax <- read_csv('https://raw.githubusercontent.com/awhstin/temperaturesv2/master/LAX-NCDC-2015-18.csv') %>% 
mutate(year = format(DATE, '%Y'),
month = factor(months(DATE),
levels = rev(c(
'一月', '二月', '三月',
'四月', '五月', '六月',
'七月', '八月', '九月',
'十月', '十一月', '十二月'
))),
TAVG = ifelse(is.na(TAVG), (TMAX + TMIN)/2, TAVG))

ggplot(lax, aes(x = TAVG,
y = month,
fill = year)) +
geom_density_ridges(scale = 0.9,
color = NA) +
hrbrthemes::theme_ipsum(base_family = 'STSong') +
scale_fill_brewer('年份', palette = 'Set2') +
labs(title = 'LAX 年平均温度',
caption = '数据来源:NCDC\nhttps://www.ncdc.noaa.gov/',
x = '温度(F)',
y = '月份')

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×