ggplot 带文本中的希腊字母

发布于 2024-08-25 01:54:43 字数 753 浏览 5 评论 0原文

我试图覆盖一些 ggplot 条中的文本以合并希腊字符。这是一些示例数据和绘图的基础。

dfr <- data.frame(
   x = rep(1:10, times = 6),
   y = runif(60),
   fx = rep(c("foo", "bar"), each = 30),
   fy = rep(c("alpha", "beta", "gamma"), each = 10, times = 2)
)

p <- ggplot(dfr, aes(x, y)) + geom_point()

我第一次尝试情节时,条带标签中没有希腊语。

 p + facet_grid(fy ~ fx)

我认为我应该向 facet_grid 添加一个标签参数来覆盖文本。我认为这应该输出一个表达式来处理希腊字符,但我的代码在打印图形时只是抛出错误。

lbl <- function(variable, value)
{
   if(variable == "fy") parse(text=as.character(value)) else value
}
p + facet_grid(fy ~ fx, labeller = lbl)


Error in aperm(X, c(s.call, s.ans)) : 
  unimplemented type 'expression' in 'aperm'

我应该如何创建条带标签?

I'm trying to override the text in some ggplot strips to incorporate Greek characters. Here's some sample data, and the base for the plot.

dfr <- data.frame(
   x = rep(1:10, times = 6),
   y = runif(60),
   fx = rep(c("foo", "bar"), each = 30),
   fy = rep(c("alpha", "beta", "gamma"), each = 10, times = 2)
)

p <- ggplot(dfr, aes(x, y)) + geom_point()

My first attempt at a plot has no Greek in the strip labels.

 p + facet_grid(fy ~ fx)

I gather that I'm supposed to add a labeller argument to facet_grid to override the text. I presumed that this should spit out an expression to handle the greek characters, but my code just throws an error when the graphic is printed.

lbl <- function(variable, value)
{
   if(variable == "fy") parse(text=as.character(value)) else value
}
p + facet_grid(fy ~ fx, labeller = lbl)


Error in aperm(X, c(s.call, s.ans)) : 
  unimplemented type 'expression' in 'aperm'

How should I be creating the strip labels?

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

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

发布评论

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

评论(2

你げ笑在眉眼 2024-09-01 01:54:44

试试这个:

p + facet_grid(fy ~ fx, labeller = label_parsed)

Try this:

p + facet_grid(fy ~ fx, labeller = label_parsed)
落在眉间の轻吻 2024-09-01 01:54:44

将其发布在这里,因为它是相关的:

如果您希望变量本身的名称以及变量的级别/值被评估为表达式(即呈现为乳胶),请尝试以下操作:

label_parseall <- function(variable, value) {
    plyr::llply(value, function(x) parse(text = paste(variable, 
        x, sep = "==")))
}

示例:

data <- data.frame(x = runif(10), y = runif(10), 
    gamma = sample(c("gamma[0]", "gamma[1]"), 10, rep = T))
ggplot(data, aes(x, y)) + geom_point() + facet_grid(~gamma, 
    labeller = label_parselabel) 

在此处输入图像描述

图像位于 http://img709.imageshack.us/img709/1168/parseall.png

posting this here since it's related:

If you want the name of the variable itself as well as the levels/values of the variable to be evaluated as an expression (i.e. rendered as if they were latex), try this:

label_parseall <- function(variable, value) {
    plyr::llply(value, function(x) parse(text = paste(variable, 
        x, sep = "==")))
}

Example:

data <- data.frame(x = runif(10), y = runif(10), 
    gamma = sample(c("gamma[0]", "gamma[1]"), 10, rep = T))
ggplot(data, aes(x, y)) + geom_point() + facet_grid(~gamma, 
    labeller = label_parselabel) 

enter image description here

image at http://img709.imageshack.us/img709/1168/parseall.png

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