本节介绍一个新包ggHoriPlot使用它可以轻松的在ggplot2中构建地平线图
参考: https://rivasiker.github.io/ggHoriPlot/
安装R包
devtools::install_github("rivasiker/ggHoriPlot")
加载R包
library(tidyverse)
library(ggHoriPlot)
library(ggthemes)
加载数据集并计算切点和原点
utils::data(climate_CPH)
cutpoints <- climate_CPH %>%
mutate(
outlier = between(
AvgTemperature,
quantile(AvgTemperature, 0.25, na.rm=T)-
1.5*IQR(AvgTemperature, na.rm=T),
quantile(AvgTemperature, 0.75, na.rm=T)+
1.5*IQR(AvgTemperature, na.rm=T))) %>%
filter(outlier)
ori <- sum(range(cutpoints$AvgTemperature))/2
sca <- seq(range(cutpoints$AvgTemperature)[1],
range(cutpoints$AvgTemperature)[2],
length.out = 7)[-4]
round(ori, 2) # The origin
#> [1] 6.58
round(sca, 2) # The horizon scale cutpoints
#> [1] -12.11 -5.88 0.35 12.81 19.05 25.28
绘制哥本哈根平均气温图
climate_CPH %>% ggplot() +
geom_horizon(aes(date_mine,
AvgTemperature,
fill = ..Cutpoints..),
origin = ori, horizonscale = sca) +
scale_fill_hcl(palette = 'RdBu', reverse = T) +
facet_grid(Year~.) +
theme_few() +
theme(
panel.spacing.y=unit(0, "lines"),
strip.text.y = element_text(size = 7, angle = 0, hjust = 0),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
panel.border = element_blank()
) +
scale_x_date(expand=c(0,0),
date_breaks = "1 month",
date_labels = "%b") +
xlab('Date') +
ggtitle('Average daily temperature in Copenhagen',
'from 1995 to 2019')
喜欢的小伙伴欢迎关注我的公众号
R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助
网友评论