R 在ggplot2字幕的plotmath表达式中包含条件元素
我正在尝试编写一个自定义函数,希望在R 在ggplot2字幕的plotmath表达式中包含条件元素,r,ggplot2,plotmath,R,Ggplot2,Plotmath,我正在尝试编写一个自定义函数,希望在ggplot2plot subtitle中显示效果大小估计及其置信区间。我正在使用plotmath正确显示希腊字母和其他数学符号 这就是我想要的字幕的两个变体的样子- 为了实现这一点,我编写了一个简单的函数- #设置 种子集(123) 图书馆(tidyverse) 图书馆(cowplot) #创建具有效果大小估计及其置信度的虚构数据帧 #间歇 effsize_df这里有一个unicode解决方案。我在dplyr中使用case\u来简化生活 # functi
ggplot2
plot subtitle中显示效果大小估计及其置信区间。我正在使用plotmath
正确显示希腊字母和其他数学符号
这就是我想要的字幕的两个变体的样子-
为了实现这一点,我编写了一个简单的函数-
#设置
种子集(123)
图书馆(tidyverse)
图书馆(cowplot)
#创建具有效果大小估计及其置信度的虚构数据帧
#间歇
effsize_df这里有一个unicode解决方案。我在dplyr
中使用case\u来简化生活
# function to prepare subtitle
subtitle_maker <- function(effsize_df, effsize.type) {
# preparing the subtitle
subtitle <-
# extracting the elements of the statistical object
base::substitute(
expr =
paste(
effsize_sym["p"]^2,
" = ",
effsize,
", 95% CI",
" [",
LL,
", ",
UL,
"]",
),
env = base::list(
effsize = effsize_df$estimate[1],
LL = effsize_df$conf.low[1],
UL = effsize_df$conf.high[1],
effsize_sym = case_when(
effsize.type == "p_eta" ~ "\U1D702",
effsize.type == "p_omega" ~ "\U1D714",
TRUE ~ NA_character_
)
)
)
# return the subtitle
return(subtitle)
}
#准备字幕的功能
subtitle_makerquote和bquote的组合会有所帮助
subtitle_maker <- function(d, type){
et <- if(type == 'a') quote(eta) else if(type == 'b') quote(omega)
bquote(.(et)['p']^2==.(d$x)~", 95% CI ["*.(d$y)*","*.(d$z)*"]")
}
d <- list(x=1,y=2,z=3)
grid::grid.newpage()
grid::grid.text(subtitle_maker(d,"a"), y=0.3)
grid::grid.text(subtitle_maker(d,"b"), y=0.7)
subtitle\u谢谢。在我接受这个答案之前,有一个简单的问题:unicode解决方案能否跨不同的操作系统工作?我总是使用Windows
机器,这是可行的,但我更希望这也适用于其他操作系统,因为我的合作者大多使用unix
机器。@indrajeetpail我想是的。我在运行Ubuntu,它运行得很好。啊,我还以为你在Windows机器上呢。不幸的是,这在Windows上不起作用。这就是您的代码所得到的:您是否有其他不涉及更改字符编码的解决方案?
subtitle_maker <- function(effsize_df, effsize.type) {
effsize.text <- if (effsize.type == "p_eta") quote(eta["p"]) else
if (effsize.type == "p_omega") quote(omega["p"])
base::substitute(
expr =
paste(effsize.text^2,
" = ",
effsize,
", 95% CI",
" [",
LL,
", ",
UL,
"]",
),
env = base::list(effsize.text = effsize.text,
effsize = effsize_df$estimate[1],
LL = effsize_df$conf.low[1],
UL = effsize_df$conf.high[1]
)
)
}