本节来解释如何使用ggh4x自带的facetted_pos_scales( )来自定义分面图形的轴刻度
加载R包
library(tidyverse)
library(ggh4x)
数据清洗
air_df <- airquality %>%
pivot_longer(cols = 1:4,names_to = "Env_vars",
values_to = "Values")
绘制分面折线图
P <- ggplot(air_df, aes(x = Day,y = Values,color = as.factor(Month),group = Month)) +
geom_point() + geom_line() +
labs(x = "Day of month", y = NULL) +
scale_color_brewer(palette = "Set1", labels = c("May","June","July","August","September")) +
facet_wrap(~Env_vars, nrow = 2,
scales = "free_y",strip.position = "left",
labeller = as_labeller(c(Temp = "Temperature (°F)",
Solar.R = "Solar radiace (lang)",
Wind = "Wind (mph)",
Ozone = "Ozone (ppb)"))) +
theme(strip.background = element_blank(),
strip.placement = "outside",
legend.position = "top",
legend.title = element_blank())
P
自定义轴刻度
P + facetted_pos_scales(
y = list(Env_vars == "Ozone" ~ scale_y_continuous(limits=c(0,200),breaks=seq(0,200,40)),
Env_vars == "Solar.R" ~ scale_y_continuous(limits=c(0, 350),breaks=seq(0,350,50)),
Env_vars == "Temp" ~ scale_y_continuous(limits=c(40, 120),breaks=seq(40,120,20)),
Env_vars == "Wind" ~ scale_y_continuous(limits=c(0,30),breaks=seq(0,30,5),
labels = letters[1:7])))
喜欢的小伙伴欢迎关注我的公众号
R语言数据分析指南,持续分享数据可视化的经典案例及一些生信知识,希望对大家有所帮助
网友评论