当前位置:网站首页>ggplot 分面的细节调整汇总
ggplot 分面的细节调整汇总
2022-07-06 21:00:00 【庄闪闪】
简介
小编前段时间介绍了 ggplot2 分面问题:R可视乎|分面一页多图。以及很多书籍都介绍了如何使用 ggplot2 的分面,如:《ggplot2: Elegant Graphics for Data Analysis》,《R语言教程》。
但是绘制之后的细节调整,几乎都没有介绍过。小编在学习的过程中,将一些细节调整技巧进行了整理,以供参考。并分享给大家,希望能帮到你~
本文框架
教程
本文通过模拟数据来举例,读者可以根据数据形式,加载自己的数据集,并进行相应的数据处理。
生成数据
# 导入包
library(ggplot2)
set.seed(1)
dat = data.frame("time" = rep(2013:2019,6),
"method" = rep(LETTERS[1:6],each = 7),
"value" = rnorm(7*6,5,1),
"upper" = rnorm(7*6,5,1) + abs(rnorm(7*6,2,0.1)),
"lower" = rnorm(7*6,5,1) - abs(rnorm(7*6,2,0.1))
)
head(dat)
基础版本绘图
cols <- c("#85BA8F", "#A3C8DC","#349839","#EA5D2D","#EABB77","#F09594") #设置颜色
p = ggplot(dat,aes(x = time, y = value,fill = method)) +
geom_line(aes(color = method)) + #添加线
geom_point(aes(color = method)) + #添加散点
geom_ribbon(aes(ymin=lower, ymax=upper), alpha=0.3) + #添加区间
scale_x_continuous(breaks = 2013:2019) +
facet_wrap(vars(method),nrow = 4) +
theme_bw() + ylab("Value") + xlab("Time") + #主题设置
theme(panel.grid = element_blank())
p
刻度尺修改
默认情况下,x、y 轴刻度是固定的,即,scales = "fixed"
。具体可以参考:https://ggplot2-book.org/facet.html。
x 轴
利用参数 scales = "free_x"
,修改 x 轴刻度尺。
p + facet_wrap(vars(method),nrow = 4,scales = "free_x")
y 轴
利用参数 scales = free_y
,修改 y 轴刻度尺。
p + facet_wrap(vars(method),nrow = 4,scales = "free_y")
双轴
利用参数 scales = free
,修改 x 和 y 轴刻度尺。
p + facet_wrap(vars(method),nrow = 4,scales = "free")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q8BdFXbf-1656937310384)(https://files.mdnice.com/user/5000/9d1e15c9-ebc3-43b2-a892-43a1229eb0b5.png)]
标题框调整
这一部分都是在 theme()
中进行调整。具体可以参考:https://ggplot2-book.org/polishing.html。
位置调整
修改 strip.position
进行位置调整。
p + facet_wrap(vars(method),nrow = 3,strip.position = "left")
去除标题框背景
利用 strip.background = element_blank()
去除标题框背景。
p + theme(
strip.background = element_blank() #去除标题框背景
)
修改标题框背景颜色
利用 strip.background = element_rect()
修改标题框背景颜色。
p + theme(strip.background=element_rect(colour="black",
fill="#2072A8"))
修改标题框文字颜色
利用 strip.text.x=element_text()
修改标题框文字颜色。
p + theme(strip.text.x=element_text(colour="white"))
删除标题框
利用 strip.text.x = element_blank()
删除标题框。
p + theme(strip.text.x = element_blank())
小编有话说
如果觉得有帮助的话,欢迎点赞,分享~
边栏推荐
- 海思万能平台搭建:颜色空间转换YUV2RGB
- API data interface of A-share index component data
- 密码学系列之:在线证书状态协议OCSP详解
- 未来发展路线确认!数字经济、数字化转型、数据...这次会议很重要
- 24.(arcgis api for js篇)arcgis api for js点修改点编辑(SketchViewModel)
- 25.(arcgis api for js篇)arcgis api for js线修改线编辑(SketchViewModel)
- Docker部署Mysql8的实现步骤
- 23.(arcgis api for js篇)arcgis api for js椭圆采集(SketchViewModel)
- codeforces每日5题(均1700)-第七天
- VHDL实现任意大小矩阵加法运算
猜你喜欢
GPT-3当一作自己研究自己,已投稿,在线蹲一个同行评议
Confirm the future development route! Digital economy, digital transformation, data This meeting is very important
Huawei and Xiaomi "copy each other"
How to customize the shortcut key for latex to stop running
VHDL实现单周期CPU设计
Implementation steps of docker deploying mysql8
PHP lightweight Movie Video Search Player source code
什么是 BA ?BA怎么样?BA和BI是什么关系?
我的勇敢对线之路--详细阐述,浏览器输入URL发生了什么
Stored procedures and functions (MySQL)
随机推荐
1200.Minimum Absolute Difference
Que savez - vous de la sérialisation et de l'anti - séquence?
注意力机制原理
[colmap] 3D reconstruction with known camera pose
VHDL implementation of arbitrary size matrix addition operation
codeforces每日5题(均1700)-第七天
维护万星开源向量数据库是什么体验
如何替换模型的骨干网络(backbone)
Decoration design enterprise website management system source code (including mobile source code)
浅谈网络安全之文件上传
腾讯云原生数据库TDSQL-C入选信通院《云原生产品目录》
Flink task exit process and failover mechanism
Top 50 hit industry in the first half of 2022
About Confidence Intervals
VHDL实现任意大小矩阵加法运算
预处理——插值
Mathematical induction and recursion
Numpy中排序操作partition,argpartition,sort,argsort
MySQL的存储引擎
Set static IP for raspberry pie