參考鏈接
- 1、https://ggplot2.tidyverse.org/reference/sec_axis.html
- 2、https://www.r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html
- 3滑负、https://github.com/alex-koiter/Weather-and-Climate-figures
- 4傻工、https://twitter.com/Alex_Koiter/status/1312458166496501760/photo/1
image.png
代碼主要來自于鏈接3
首先是準(zhǔn)備數(shù)據(jù)的代碼
library(tidyverse)
library(lubridate)
#install.packages("devtools")
#install.packages("cli")
#library(devtools)
devtools::install_github("ropensci/weathercan")
#install.packages("weathercan")
library(weathercan)
library(patchwork)
stations_search(name = "brandon")
# Download daily weather data for 2020
df_day <- weather_dl(station_ids = 49909,
interval = "day",
start = "2020-01-01",
end = "2020-12-31")
# download hourly weather data
df_hour <- weather_dl(station_ids = 49909,
interval = "hour",
start = "2020-09-18",
end = "2020-09-28")
# Download daily climate normals
df_normal <- normals_dl(climate_ids = 5010480) %>%
unnest(normals) %>%
filter(period != "Year") %>%
select(period, temp_daily_average, precip) %>%
mutate(date = mdy(paste0(period, "-15-2020")))
# Calculate 2020 monthly precip totals
df_month <- df_day %>%
group_by(month) %>%
summarise(precip = sum(total_precip, na.rm = TRUE)) %>%
mutate(date = mdy(paste0(month, "-15-2020")))
這部分代碼大家可以自己試著運行一下多柑,我用R4.0.3版本遇到的報錯,沒有找到解決辦法链嘀,換成R4.1.0之后運行成功了
我將示例數(shù)據(jù)保存下來了懂版,如果以上代碼沒有運行成功鹃栽,可以在公眾號獲取數(shù)據(jù),保存數(shù)據(jù)的代碼
save(df_day,df_hour,df_normal,df_month,
file = "20211121.Rdata")
現(xiàn)在用20211121.Rdata 作為開始
首先是讀取數(shù)據(jù)集
load("20211121.Rdata")
第一個圖是用到df_normal這個數(shù)據(jù)集
df_normal
dim(df_normal)
首先是一個柱形圖躯畴,但這里的柱形圖是通過geom_segment()
函數(shù)實現(xiàn)的
library(ggplot2)
library(lubridate)
作圖
ggplot() +
theme_bw() +
geom_segment(data = df_normal,
aes(x = date,
y = precip/3 - 30,
xend = date,
yend = -30),
size = 8,
colour = gray(0.5))
image.png
對x軸操作的代碼
這里涉及到時間格式的數(shù)據(jù)如何操作
ggplot() +
theme_bw() +
geom_segment(data = df_normal,
aes(x = date,
y = precip/3 - 30,
xend = date,
yend = -30),
size = 8,
colour = gray(0.5))+
scale_x_date(date_labels = "%b",
date_breaks = "1 month",
expand = c(0.01,0.01),
name = "",
limits = (c(as_date("2020-01-01"),
as_date("2020-12-31"))))
image.png
接下來就是對Y軸操作民鼓,添加雙坐標(biāo)軸的代碼
ggplot() +
theme_bw() +
geom_segment(data = df_normal,
aes(x = date,
y = precip/3 - 30,
xend = date,
yend = -30),
size = 8,
colour = gray(0.5))+
scale_x_date(date_labels = "%b",
date_breaks = "1 month",
expand = c(0.01,0.01),
name = "",
limits = (c(as_date("2020-01-01"),
as_date("2020-12-31")))) +
scale_y_continuous(name = expression("Temperature " ( degree*C)),
sec.axis = sec_axis(~ (. + 30) * 3 ,
name = "Precipitation (mm)",
breaks = seq(0,182,20)),
limits = c(-30, 30),
expand = c(0, 0))
image.png
這里有一個小知識點是如果要用攝氏度那個符號薇芝,他的寫法是expression("Temperature " ( degree*C))
添加擬合曲線的代碼
ggplot() +
theme_bw() +
geom_segment(data = df_normal,
aes(x = date,
y = precip/3 - 30,
xend = date,
yend = -30),
size = 8,
colour = gray(0.5))+
scale_x_date(date_labels = "%b",
date_breaks = "1 month",
expand = c(0.01,0.01),
name = "",
limits = (c(as_date("2020-01-01"),
as_date("2020-12-31")))) +
scale_y_continuous(name = expression("Temperature " ( degree*C)),
sec.axis = sec_axis(~ (. + 30) * 3 ,
name = "Precipitation (mm)",
breaks = seq(0,182,20)),
limits = c(-30, 30),
expand = c(0, 0))+
stat_smooth(data = df_normal,
aes(x = date,
y = temp_daily_average),
method = "loess",
formula = 'y~x',
se = FALSE,
size = 1,
colour = gray(0.5))
image.png
最后是添加了一個文本注釋
ggplot() +
theme_bw() +
geom_segment(data = df_normal,
aes(x = date,
y = precip/3 - 30,
xend = date,
yend = -30),
size = 8,
colour = gray(0.5)+
scale_x_date(date_labels = month.abb,
date_breaks = "1 month",
expand = c(0.01,0.01),
name = "",
limits = (c(as_date("2020-01-01"),
as_date("2020-12-31")))) +
scale_y_continuous(name = expression("Temperature " ( degree*C)),
sec.axis = sec_axis(~ (. + 30) * 3 ,
name = "Precipitation (mm)",
breaks = seq(0,182,20)),
limits = c(-30, 30),
expand = c(0, 0))+
stat_smooth(data = df_normal,
aes(x = date,
y = temp_daily_average),
method = "loess",
formula = 'y~x',
se = FALSE,
size = 1,
colour = gray(0.5))+
annotate(geom="text",
x = as_date("2020-03-15"),
y = 25,
label = "1981-2010 Climate Normals",
color="black")
image.png
文章開頭提到的參考鏈接3里還有3幅圖的代碼,大家可以自己試著重復(fù)一下
完整作圖代碼
x1<-tidyquant::palette_dark()
cols<-matrix(x1)[,1]
pdf(file = "p1.pdf",
width = 9.4,
height = 4,
family = "serif")
ggplot() +
theme_bw() +
geom_segment(data = df_normal,
aes(x = date,
y = precip/3 - 30,
xend = date,
yend = -30),
size = 8,
colour = cols)+
scale_x_date(date_labels = month.abb,
date_breaks = "1 month",
expand = c(0.01,0.01),
name = "",
limits = (c(as_date("2020-01-01"),
as_date("2020-12-31")))) +
scale_y_continuous(name = expression("Temperature " ( degree*C)),
sec.axis = sec_axis(~ (. + 30) * 3 ,
name = "Precipitation (mm)",
breaks = seq(0,182,20)),
limits = c(-30, 30),
expand = c(0, 0))+
stat_smooth(data = df_normal,
aes(x = date,
y = temp_daily_average),
method = "loess",
formula = 'y~x',
se = FALSE,
size = 1,
colour = gray(0.5))+
annotate(geom="text",
x = as_date("2020-03-15"),
y = 25,
label = "1981-2010 Climate Normals",
color="black")
dev.off()
image.png
歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本
小明的數(shù)據(jù)分析筆記本 公眾號 主要分享:1丰嘉、R語言和python做數(shù)據(jù)分析和數(shù)據(jù)可視化的簡單小例子恩掷;2、園藝植物相關(guān)轉(zhuǎn)錄組學(xué)供嚎、基因組學(xué)、群體遺傳學(xué)文獻閱讀筆記峭状;3克滴、生物信息學(xué)入門學(xué)習(xí)資料及自己的學(xué)習(xí)筆記!