当前位置:网站首页>如何在分面中添加数学表达式标签?
如何在分面中添加数学表达式标签?
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()
此时绘制出来的结果如下:

边栏推荐
猜你喜欢
![[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios](/img/c7/3faa29dc374e2e16e59ac9b8ebf249.png)
[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios

Promise笔记(一)

nodejs environment variable settings

481-82(105、24、82、34、153)

ISELED---the new choice of ambient lighting scheme

Flask introductory learning tutorial

xxl-job源码解析(技术分享)

LeetCode-283-移动零

The Prospects of the Metaverse and the Four Tracks

二、判断 & 循环
随机推荐
Store Limit usage documentation
【重磅来袭】教你如何在RGBD三维重建中获取高质量模型纹理
MySql 和 PostgreSQL 数据库 根据一张表update另一张表数据
Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
Promise笔记(一)
Redis 复习计划 - Redis 数据结构和持久化机制
STM32F407定时器输入捕获
[AGC] Quality Service 1 - Example of Crash Service
php如何去除字符串最后一位字符
TiUP FAQ
TensorFlow custom training function
tiup clean
Excel uses Visual Basic Editor to modify macros
PMP每日一练 | 考试不迷路-7.30(包含敏捷+多选)
Nature Microbiology综述:聚焦藻际--浮游植物和细菌互作的生态界面
Changing SELECT...FROM to FROM...SELECT doesn't 'fix' SQL
应用OPC解决方案实现控制系统数据的安全交换
被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
工具| execsnoop 短时进程追踪工具
How to implement timing tasks for distributed applications in Golang