R:以编程方式生成具有上标的地块轴断裂标签(强调编程方式)

时间:2022-02-15 15:00:02

I would like to make tick labels with superscripts as in mylabels in the following example. However, I don't want to hand code them, I want them to go up to 3^LEVELS, where LEVELS is a constant

在下面的例子中,我想在mylabel中使用超级脚本标记标签。然而,我不想手工编写他们,我希望他们去3 ^水平,水平是一个常数

library(ggplot2)

LEVELS = 4

mylabels = c(
  expression(paste(3^4,"= 81")),
  expression(paste(3^3,"= 27")),
  expression(paste(3^2,"= 9")),
  expression(paste(3^1,"= 3")),
  expression(paste(3^0,"= 1")))
mylabels
length(mylabels)


df=data.frame(x=runif(40),y=(runif(40)*10)%%5)
p = ggplot(df,aes(x,y))  + 
  geom_point() + 
  scale_y_continuous(breaks = 0:LEVELS, labels = mylabels)
p

The hard coded verison works perfectly. But I can't seem to get it programatically. The following:

硬编码的verison非常好用。但我似乎无法从程序上理解它。以下几点:

mylabels=c(paste("expression(paste(3^",LEVELS:0,'," = ',3^(LEVELS:0),'"))',sep=""))

doesn't properly evaluate in the chart (e.g., it writes the word 'expression', etc. in the label). It creates:

在图表中没有正确的评估(例如,它在标签中写了单词“expression”等)。它创建:

[1] "expression(paste(3^4,\" = 81\"))" "expression(paste(3^3,\" = 27\"))" "expression(paste(3^2,\" = 9\"))" 
[4] "expression(paste(3^1,\" = 3\"))"  "expression(paste(3^0,\" = 1\"))" 

and what I want is:

我想要的是:

expression(paste(3^4, "= 81"), paste(3^3, "= 27"), paste(3^2, 
"= 9"), paste(3^1, "= 3"), paste(3^0, "= 1"))

Have messed about with collapse, noquote, eval, sprintf, etc.

在崩溃、noquote、eval、sprintf等方面做过手脚。

1 个解决方案

#1


3  

You need to build the expression vector as a character vector first, and then parse it using parse(), which returns an expression vector correspondent to the input character vector. Building the character vector is best done with sprintf():

您需要首先将表达式向量构建为字符向量,然后使用parse()解析它,后者返回与输入字符向量对应的表达式向量。构建字符向量最好使用sprintf():

mylabels <- parse(text=sprintf('paste(3^%d,\' = %d\')',LEVELS:0,3^(LEVELS:0)));
mylabels;
## expression(paste(3^4,' = 81'), paste(3^3,' = 27'), paste(3^2,' = 9'),
##     paste(3^1,' = 3'), paste(3^0,' = 1'))

R:以编程方式生成具有上标的地块轴断裂标签(强调编程方式)


Other tips:

其他建议:

1: When providing sample code that depends on random number generation, please call set.seed() before generating any randomness.

1:当提供依赖于随机数生成的示例代码时,请在生成任何随机性之前调用set.seed()。

2: The expression() function is variadic, and returns an expression vector whose elements correspond to the input arguments. Hence you can replace

2: expression()函数是variadic,返回一个表达式向量,其元素与输入参数一致。因此你可以换

c(
    expression(paste(3^4,"= 81")),
    expression(paste(3^3,"= 27")),
    expression(paste(3^2,"= 9")),
    expression(paste(3^1,"= 3")),
    expression(paste(3^0,"= 1"))
)

with

expression(
    paste(3^4,"= 81"),
    paste(3^3,"= 27"),
    paste(3^2,"= 9"),
    paste(3^1,"= 3"),
    paste(3^0,"= 1")
)

3: Avoid redundant calls to c(). You can replace

3:避免对c()的冗余调用。你可以换

c(paste("expression(paste(3^",LEVELS:0,'," = ',3^(LEVELS:0),'"))',sep=""))

with

paste("expression(paste(3^",LEVELS:0,'," = ',3^(LEVELS:0),'"))',sep="")

#1


3  

You need to build the expression vector as a character vector first, and then parse it using parse(), which returns an expression vector correspondent to the input character vector. Building the character vector is best done with sprintf():

您需要首先将表达式向量构建为字符向量,然后使用parse()解析它,后者返回与输入字符向量对应的表达式向量。构建字符向量最好使用sprintf():

mylabels <- parse(text=sprintf('paste(3^%d,\' = %d\')',LEVELS:0,3^(LEVELS:0)));
mylabels;
## expression(paste(3^4,' = 81'), paste(3^3,' = 27'), paste(3^2,' = 9'),
##     paste(3^1,' = 3'), paste(3^0,' = 1'))

R:以编程方式生成具有上标的地块轴断裂标签(强调编程方式)


Other tips:

其他建议:

1: When providing sample code that depends on random number generation, please call set.seed() before generating any randomness.

1:当提供依赖于随机数生成的示例代码时,请在生成任何随机性之前调用set.seed()。

2: The expression() function is variadic, and returns an expression vector whose elements correspond to the input arguments. Hence you can replace

2: expression()函数是variadic,返回一个表达式向量,其元素与输入参数一致。因此你可以换

c(
    expression(paste(3^4,"= 81")),
    expression(paste(3^3,"= 27")),
    expression(paste(3^2,"= 9")),
    expression(paste(3^1,"= 3")),
    expression(paste(3^0,"= 1"))
)

with

expression(
    paste(3^4,"= 81"),
    paste(3^3,"= 27"),
    paste(3^2,"= 9"),
    paste(3^1,"= 3"),
    paste(3^0,"= 1")
)

3: Avoid redundant calls to c(). You can replace

3:避免对c()的冗余调用。你可以换

c(paste("expression(paste(3^",LEVELS:0,'," = ',3^(LEVELS:0),'"))',sep=""))

with

paste("expression(paste(3^",LEVELS:0,'," = ',3^(LEVELS:0),'"))',sep="")