当前位置:网站首页>如何在分面中添加数学表达式标签?
如何在分面中添加数学表达式标签?
2022-07-30 15:29:00 【庄闪闪】
简介
最近自己论文中需要绘制不同公式得到结果的分面图,并且在标签中给出表达式。对于这样的需求,一时半会想不出来。经过一顿搜索,得到了以下不错的解决方案。
关于分面的基础教程可见以前写的推文:R可视乎|分面一页多图。拓展的技巧可见:xxx。老俊俊的生信笔记也给出了几篇不错的分面技巧介绍:使用 jjAnno 对分面添加注释;ggplot2 如何在不同分面添加不同文字;ggplot2 如何在不同分面添加不同图形。
构建模拟数据
老样子,给出模拟数据,读者根据自己数据进行转换即可。
# 相关包
library(ggplot2)
library(grid)
mydf <- data.frame(letter = factor(rep(c("A", "B", "C", "D"), each = 20)), x = rnorm(80), y = rnorm(80))
head(mydf)

基本绘图
简单绘制出数据 x,y 之间的散点图和拟合曲线,并根据分类数据 letter 进行分面。
ggplot(mydf, aes(x = x, y = y)) +
geom_smooth(method = "lm") +
geom_point() +
facet_wrap(~ letter)

设置表达式标签
使用 expression() 函数定义表达式标签,存储到 f_names。并定义 f_labeller 提取 f_names 中的值。之后在分面中使用参数 labeller = f_labeller。
当然,也可以使用
latex2exp包中的TeX(), 用类似 LeTeX 语句写表达式。例如:TeX("$\\alpha_2$")。
f_names <- list('A' = expression(paste(alpha[2])), 'B' = expression(Gamma(3,4)), 'C' = expression(paste(y = beta[0] + beta[1]*x[1])), 'D' = expression(delta))
f_labeller <- function(variable, value){return(f_names[value])}
ggplot(mydf, aes(x = x, y = y)) +
geom_smooth(method = "lm") +
geom_point() +
facet_wrap(~ letter, labeller = f_labeller)

修改主题
ggplot(mydf, aes(x = x, y = y)) +
geom_smooth(method = "lm",color = "#e99e9c",fill = "#98c0d7") +
geom_point(color = "gray60") +
facet_wrap(~ letter, labeller = f_labeller,scales = "free") +
theme_bw() + ylab("Value") + xlab("Time") + #主题设置
theme(panel.grid = element_blank(),
strip.background = element_blank())

添加中文标签
加入想要添加中文标签,只需修改:
# 设置标签
f_names <- list('A' = expression(paste(alpha[2])), 'B' = expression(Gamma(3,4)), 'C' = expression(paste(y = beta[0] + beta[1]*x[1])), 'D' = expression(delta))
f_names <- list('A' = "庄闪闪", 'B' = "庄亮亮",
'C' = "庄晶晶", 'D' = "庄暗暗")
如果存在中文字体乱码,请参考这篇推文:R问题|解决PDF导出字体无法显示的问题。即在文中前面加入以下代码:
library(showtext)
showtext.auto()
此时绘制出来的结果如下:

边栏推荐
- 【HMS core】【FAQ】push kit, WisePlay DRM, Location Kit, Health Kit, 3D Modeling Kit, SignPal Kit Typical Questions Collection 4
- Troubleshooting TiUP
- Manage components using TiUP commands
- EST综述:eDNA的多种状态以及在水环境中持久性的认知
- arcpy使用教程
- 代码随想录笔记_哈希_1l两数之和
- Placement Rules 使用文档
- How to split microservices?
- 后浪来袭!阿里产出“第二代”容器技术手册及脑图,这也太香了吧
- 数据库-SQL
猜你喜欢

动态规划 --- 状态压缩DP 详细解释

几种常见的存储器

应用接入华为分析在应用调试模式下为何没有数据上报?

【HMS core】【Media】【Video Editing Service】 The online material cannot be displayed, it is always in the loading state or the network is abnormal

rscsa笔记八

Delayed message queue

Google engineer "code completion" tool; "Transformers NLP" accompanying book code; FastAPI development template; PyTorch model acceleration tool; cutting-edge papers | ShowMeAI News Daily

FME读写cass数据的方案及操作流程

timed task corn

ISELED---the new choice of ambient lighting scheme
随机推荐
Sleuth+Zipkin (visualization) service link tracking
Classes and Objects (Part 2)
二、判断 & 循环
[HMS core] [FAQ] A collection of typical questions about push kit, analysis services, and video editing services 3
开源WebGIS架构
【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
类和对象(下篇)
Packages - Notes
Flask introductory learning tutorial
LeetCode-283-移动零
STM32F407定时器输入捕获
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
How to remove last character from string in php
Mysql database query is very slow. Besides the index, what else can be caused?
nodejs环境变量设置
TiDB 工具适用场景
【AGC】质量服务1-崩溃服务示例
后浪来袭!阿里产出“第二代”容器技术手册及脑图,这也太香了吧
代码随想录笔记_哈希_1l两数之和
tiup clean