【问题标题】:R: Complex Axis Format ExpressionR:复轴格式表达式
【发布时间】:2017-03-15 18:48:07
【问题描述】:

我正在使用 ggplot2 并尝试完全使用粘贴和表达式来合并复杂的表达式。

例如,我试图将 0.5e-6 的值显示为 0.5 微秒,并在 y 轴标签中显示(5 x 10^-7 秒)。

到目前为止,我可以做到其中任何一个,但不能同时做到这两个。下面给出了一个最小的工作示例。

library(ggplot2)

dat <- data.frame(
  A = factor(c("O", "O", "P", "P", "Q", "Q", "O", "O", "P", "P", "Q", "Q"), levels=c("O", "O", "P", "P", "Q", "Q","O", "O", "P", "P", "Q", "Q")),
  B = factor(c("P-0.1", "P-0.1", "P-0.1", "P-0.1","P-0.1", "P-0.1",  "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2"), levels = c("P-0.1", "P-0.1", "P-0.1", "P-0.1","P-0.1", "P-0.1",  "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2", "P-0.2")),
  X = c( 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1),
  Y = c(1e-6, 1.5e-6, 1.2e-6, 1.3e-6, 0.9e-6, 1.4e-6, 3.0e-6, 2.0e-6, 3.2e-6, 2.1e-6, 2.7e-6, 1.9e-6)
)

fancy_scientific_text <- function(l) {
  # turn in to character string in scientific notation
  l <- format(l, scientific = TRUE)
  # quote the part before the exponent to keep all the digits
  l <- gsub("^(.*)e", "'\\1'e", l)
  l <- gsub("e\\+","e",l)
  # turn the 'e+' into plotmath format
  l <- gsub("e", "%*%10^", l)
  # print (l)
  l <- gsub("\\'1[\\.0]*\\'\\%\\*\\%", "", l)
  l <- gsub("\\'0[\\.0]*\\'\\%\\*\\%10\\^00", "0", l)
  return(l)
}

fancy_scientific <- function(l) {
  # return this as an expression
  parse(text=fancy_scientific_text(l))
}


human_time_format <- function(y){
  if (!is.na(y)){
    substitute(paste(m, " ", mu, "s", sep=""), list(m=y*1e6))
  }
}

human_times <- function(x = NULL, smbl ="sec"){
  sapply(x, human_time_format)
}

human_time_format_combined <- function(y){
  if (!is.na(y)){
    substitute(paste(y_lab, " (", m, " ", mu, "s)", sep=""), list(m=y*1e6, y_lab=fancy_scientific(y)))
  }
}

human_times_combined <- function(x = NULL, smbl ="sec"){
  sapply(x, human_time_format_combined)
}



p = ggplot(data=dat, aes(x=X, y=Y, colour=A, size=A, shape=A, linetype=A, fill=B, group=interaction(A,B))) + geom_point() + geom_line() + theme_bw()
p = p + geom_point(size=4, alpha=0) + geom_point(size=4, show.legend=FALSE) + guides(shape = guide_legend(nrow=3, byrow = TRUE, keywidth = 1.5, keyheight = 1), colour = guide_legend(override.aes = list(alpha=1)))

p = p + scale_shape_manual(name="", values=c(21,22,23))
p = p + scale_colour_manual(name="", values=c("#005ccc", "#007700", "#56B4E9"))
p = p + scale_linetype_manual(name="", values=c(0,0,1))
p = p + scale_size_manual(name="", values = c(1, 1, 1))
p = p + scale_fill_manual(name = "", values = c("red", "blue"), guide = guide_legend(override.aes = list(shape = 22, size = 5)))

p0 = p + ggtitle("p0")
p1 = p + scale_y_continuous(name = "Y", labels = fancy_scientific) + ggtitle("p1")
p2 = p + scale_y_continuous(name = "Y", labels = human_times) + ggtitle("p2")
p3 = p + scale_y_continuous(name = "Y", labels = human_times_combined) + ggtitle("p3")

这是输出:

p0 是未格式化的版本。 p1 是科学格式的版本,p2 是公制单位格式的版本,p3 是既具有 p1 格式又具有 p2 格式的预期格式。但我无法捕捉到这里的科学格式。

【问题讨论】:

  • 请提供正确的reproducible example,其中包含示例输入数据和简单代码,我们可以运行以尝试可能的解决方案。显示您尝试使用这些表达式的位置。这将使您更容易为您提供帮助。
  • 也许你只想要substitute(paste("(", exp_lbl, ")", m, " ", mu, sm, sep = ""), list(m=y*1e6, sm = smbl, exp_lbl=fancy_scientific(y)[[1]]))。同样,不确定,因为您没有提供测试方法。
  • 好的,我会尽快添加一个可重现的示例。
  • @MrFlick 我想要一些不同的东西。我将添加一个简单的示例,很快就会澄清。我明白我想要的东西还不够清楚。
  • 嗨@MrFlick,我添加了一个可重现的示例。请看一看。

标签: r ggplot2 axis-labels


【解决方案1】:

这基本上就是我在评论中的内容。您不能在另一个表达式中嵌入“表达式”。您需要在表达式中组合调用。您可以通过索引获取表达式的内容。因此,如果您更改human_time_format_combined 函数以提取由fancy_scientific 返回的表达式的内容,您将一切就绪

human_time_format_combined <- function(y){
  if (!is.na(y)){
    substitute(paste(y_lab, " (", m, " ", mu, "s)", sep=""), 
        list(m=y*1e6, y_lab=fancy_scientific(y)[[1]]))
  }
}

然后p3 将返回

还请注意,您通常不需要paste(),因为它并没有完全按照您在?plotmath 表达式的上下文中的作用。它通常可以替换为*。例如

human_time_format <- function(y){
  if (!is.na(y)){
    substitute(m*mu*"s", list(m=y*1e6))
  }
}

human_time_format_combined <- function(y){
  if (!is.na(y)){
    substitute(y_lab~~(m*" "*mu*"s"), list(m=y*1e6, y_lab=fancy_scientific(y)[[1]]))
  }
}

【讨论】:

  • 非常感谢。这很有启发性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多