如何在ggplot2中将字符串参数传递给表达式函数? [英] How to pass a string argument to expression function in ggplot2?

查看:46
本文介绍了如何在ggplot2中将字符串参数传递给表达式函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在围绕ggplot2编写包装函数,并且对传递的字符串参数之一有困难.这是示例代码

I'm writing a wrapper function around ggplot2 and having difficulty with one of the string arguments passed. Here's the sample code

myPlot <- function(tim, labx){
  ggplot(subset(dat,TIME=tim), aes(x=WT, y=Var))+
    geom_point(size=2)+
    facet_wrap(~Dose)+
    scale_x-continuous(expression(bold("Predicted"~labx~"Concentration ("*mu*"g/mL)")))
}

当我说 myplot(100,"Week3")时,我的x轴标签显示为预计的Labx浓度(µg/mL)",而不是预计的Week3浓度(µg/mL)")".我该如何解决?

When I say myplot(100, "Week3"), my x-axis label is showing as "Predicted labx Concentration (µg/mL)" instead of "Predicted Week3 Concentration (µg/mL)". How do I fix this?

推荐答案

一种解决方案是使用 bquote()代替 expression(),并使用.() bquote 内以评估字符(字符串)变量.

One solution is to use bquote() instead of expression(), and use .() inside of bquote to evaluate character (string) variables.

下面是该问题的完全可重现的示例.

Below is a fully reproducible example of the issue.

library(ggplot2)

labx = "Week3"

p = ggplot(data.frame(x=1:5, y=1:5), aes(x, y)) + 
    geom_point() + 
    xlab(bquote(bold(Predicted~.(labx)~Concentration~(mu*g/mL))))
p

这篇关于如何在ggplot2中将字符串参数传递给表达式函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆