【发布时间】:2013-11-02 23:45:22
【问题描述】:
当为情节标题提供plotmath 表达式时,我认为paste 的工作方式与paste 一样,但现在我不确定。它是plotmath 函数的特殊参数吗?在 plotmath 中,paste() 的行为类似于 paste0() 和 paste0 被直接引用,而 paste() 的 sep 参数被忽略,但未在表达式中引用。 paste() 是如何解释的?请参阅下面的四个示例:
# Data to plot
x <- seq( 55 , 70 , length = 2001 )
probs <- dexp( x , rate = 5 / 60 )
s <- sample( x , 10000 , prob = probs , repl = TRUE )
# Some plots
par(mfrow = c(2,2) )
#1) paste() behaves like paste0() here
hist( s , breaks = 30 ,
main = expression( paste( "1: Sampling of" , Phi , "Distribution" ) ) ,
xlab = "No spaces in title using paste()" , axes = F )
#2) add spaces in the expression where they are needed
hist( s , breaks = 30 ,
main = expression( paste( "2: Sampling of " , Phi , " Distribution" ) ) ,
xlab = "Single spaces in titles: paste() behaves like paste0()" , axes = F )
#3) Don't put spaces, use a sep argument - ignored
hist( s , breaks = 30 ,
main = expression( paste( "3: Sampling of" , Phi , "Distribution" , sep = " " ) ) ,
xlab = "No spaces using paste(), `sep` argument is ignored but not quoted in expression" , axes = F )
#4) paste0 is not recognised
hist( s , breaks = 30 ,
main = expression( paste0( "4: Sampling of " , Phi , " Distribution" ) ) ,
xlab = "paste0() not recognised" , axes = F )
【问题讨论】:
-
你可能需要
eval(expression(paste...))--- 至少我在控制台级别是这样做的以返回一个字符字符串。 -
@CarlWitthoft 说我的情节 1 的示例代码?我发现 R 会搜索
Phi对象(Phi 是一个绘图功能,应该被解释为符号)。 -
哼。我似乎记得一个关于如何做你想做的事的教程,但不知道它在哪里。
-
@CarlWitthoft:您的评论暴露了对绘图表达式的根本误解。如果您想使用
text将字符串打印到绘图设备,只需将其括在引号中即可。如果你想用显示的空格连接表达式或字符串,请使用~。在 ?plotmath 页面中使用paste和插入的 " " 的示例并不真正需要该功能,可以更紧凑地写为:text(8, 3, expression(frac(1, sigma*sqrt(2*pi))~~e^{ frac( -(x-mu)^2, 2*sigma^2)}), cex = 1.2) -
@DWin,好吧,我确实说我完全忘记了——其中包括
?plotmath页面:-)。但是感谢您在评论之前提醒我 rtfm;这是我应得的。
标签: r graphics syntax plot plotmath