将表达式对象组合到单个文本字符串中,用于ggplot标签

发布于 2025-02-02 05:46:20 字数 589 浏览 1 评论 0原文

我正在尝试在GGPLOT中创建标签,该标签由字符矢量和表达式组成,并将其组合成单个标签。我希望这些字符和表达式来自对象,以便我可以轻松地使用函数将它们交换为使用GGPLOT创建的不同图。

不幸的是,我缺乏结合存储在对象中的表达式所需的语法知识。我的情况如下所述:

library(ggplot2)

data(iris)

sup <- bquote(super^1)
sub <- bquote(sub[1])

ggplot() +
geom_point(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
labs(x = expression('Static text: '~sup~sub))

我尝试了bquote(),quote(),替代()和我尝试表达式(粘贴('主文本',sup,sub)的组合。我还尝试了eval()函数以查看 它将迫使对象作为表达式中的表达式读取。

如果由于我对语法的了解有限, 堆栈溢出是我要学习这些东西的地方

因此,

。 sstatic.net/qpi3g.png“ rel =“ nofollow noreferrer”>

I am trying to create labels in ggplot that are comprised of character vectors and expressions combined into a single label. I want the characters and expressions to be sourced from objects so that I can easily use a function to swap them for different plots created using ggplot.

Unfortunately I lack the syntax knowledge needed to combine expressions stored in objects. My situation is described below:

library(ggplot2)

data(iris)

sup <- bquote(super^1)
sub <- bquote(sub[1])

ggplot() +
geom_point(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
labs(x = expression('Static text: '~sup~sub))

I have tried combinations of bquote(), quote(), substitute(), and I tried expression(paste('Main text', sup, sub). I also tried the eval() function to see if it would force the objects to be read as expressions inside the expression.

Because of my limited knowledge of syntax, I don't know what other options are available. It has been difficult to find advanced R resources that explain syntax for such specific situations so stack overflow has been the place I go to learn these things.

My main goal is to make it look like this, but importing text from an object instead of writing directly into the expression:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-02-09 05:46:20

我们可以将其包装在bquote

library(ggplot2)
ggplot() +
 geom_point(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
   labs(x = bquote('Static text: '~.(sup)~.(sub)))

-output

”在此处输入图像描述”

We can wrap it within bquote

library(ggplot2)
ggplot() +
 geom_point(data = iris, aes(x = Sepal.Length, y = Sepal.Width)) +
   labs(x = bquote('Static text: '~.(sup)~.(sub)))

-output

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文