Plotmath表达式向量的替代方案

时间:2015-10-14 18:39:55

标签: r r-grid plotmath

我试图在每隔3个刻度标记一个轴,其值为10 ^ n,格式为上标。我正在使用grid图形。我正在做一些事情。这是一个MWE(或者我看到有一些动作称之为代表):

library("grid")
grid.newpage()
axisVP <- viewport(width = unit(6, "inches"), height = unit(0.01, "inches"))
pushViewport(axisVP)
labs <- c(1, "", "", expression(10^{-3}), "", "", expression(10^{-6}), "", "",
   expression(10^{-9}), "", "", expression(10^{-12}))
grid.xaxis(at = seq(0, 1, length.out = 15)[-c(1,15)], label = labs)

但是,这仅标记第一个刻度线1.查看str(labs)它是一个长度为13的表达式,如预期的那样。表达式被grid.xaxis接受。所以我不太清楚为什么只显示第一个值。我已经在SO上探讨了相关问题,但大多数似乎都将单个表达式作为轴标签或标题处理,而不是一系列表达式。大多数处理轴标签的问题都是使用专门的函数标记每个标记。

1 个答案:

答案 0 :(得分:2)

这就是我要做的事情:

library(grid)
grid.newpage()
axisVP <- viewport(width = unit(6, "inches"), height = unit(0.01, "inches"))
pushViewport(axisVP)

## First plot an axis with all ticks and no labels
ticks_at <- seq(1/15, 14/15, length.out=13)
grid.xaxis(at = ticks_at, label=FALSE)

## Then add labels where you want them
labs <- parse(text=c("1", "10^{-3}", "10^{-6}", "10^{-9}", "10^{-12}"))
## Alternatively, use this, though in practice it's more cumbersome
# labs <- c(expression(1), expression(10^{-3}), expression(10^{-6}), 
#           expression(10^{-9}), expression(10^{-12}))
labs_at <- seq(1/15, 14/15, length.out=5)
grid.xaxis(at = labs_at, label = labs)

enter image description here