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

边栏推荐
- golang modules initialization project
- tiup help
- 开源WebGIS架构
- 影像信息提取DEM
- 481-82 (105, 24, 82, 34, 153),
- Mysql database query is very slow. Besides the index, what else can be caused?
- [Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios
- 被捧上天的Scrum敏捷管理为何不受大厂欢迎了?
- php如何查询字符串出现位置
- 【HMS core】【FAQ】push kit, WisePlay DRM, Location Kit, Health Kit, 3D Modeling Kit, SignPal Kit Typical Questions Collection 4
猜你喜欢

Promise笔记(一)
![[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios](/img/c7/3faa29dc374e2e16e59ac9b8ebf249.png)
[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios

【AGC】质量服务2-性能管理示例

RISC-V calling conventions

Redis 复习计划 - Redis 数据结构和持久化机制

【AGC】开放式测试示例

Mysql database query is very slow. Besides the index, what else can be caused?

几种常见的存储器

arcpy使用教程

Huawei ADS reports an error when obtaining conversion tracking parameters: getInstallReferrer IOException: getInstallReferrer not found installreferrer
随机推荐
Back waves are coming!Ali produced the "second generation" container technical manual and brain map, which is too fragrant
【HMS core】【FAQ】push kit、AR Engine、广告服务、扫描服务典型问题合集2
TiDB tool download
Changing SELECT...FROM to FROM...SELECT doesn't 'fix' SQL
【AGC】Open Test Example
MySql 和 PostgreSQL 数据库 根据一张表update另一张表数据
Flask入门学习教程
481-82(105、24、82、34、153)
[Cloud Native] Service Industry Case - Solutions for Unpredictable Concurrency Scenarios
Data Analysis Tools - DDL operations & DML operations in HQL
Flask introductory learning tutorial
【HMS core】【FAQ】push kit、WisePlay DRM、Location Kit、Health Kit、3D Modeling Kit、SignPal Kit典型问题合集4
Applicable scenarios of TiDB tools
服务器装好系统的电脑怎么分区
tiup clean
tiup clean
tiup help
How to do a good job in technology selection
QIIME2得到PICRUSt2结果后如何分析
[HMS core] [FAQ] Collection of typical problems of push kit, AR Engine, advertising service, scanning service 2