R - 如何为一个图例元素组合 expression()、paste() 和 formatC() 命令?

标签 r

我正在尝试建立一个美好的传奇。它应该包含希腊字母 mu,我可以使用表达式、一些文字(即“:”和“mm”)以及使用 formatC 格式化的模型系数来完成此操作。 我运行没有问题的是要么没有希腊字母,然后我可以使用简单的粘贴命令

leg.txt <- c("Number of Points:"
           ,formatC(length(dist),big.mark=" ")
           ,"Normal distribution"
           ,paste(mu,":",formatC(fit$estimate[1], digits = 3,format = "f"),"mm")
           ,paste(expression(sigma),":",formatC(fit$estimate[2], digits = 3,format = "f"),"mm")
           ,"Density plot"
           ,paste(expression(bar(x)),":",formatC(mean, digits = 3,format = "f"),"mm")
           ,paste("SD:",formatC(sd, digits = 3,format = "f"),"mm")
           )


legend("topleft",  leg.txt
     ,col= c("white","white","blue","white","white","red","white","white")
     ,bty = "n"
     ,lwd = c(2,2)  

)

这会导致类似的结果

(由于声誉问题无法发布图片)

μ:0.283毫米

但是可以看出,没有包含希腊字母。切换到类似的内容

leg.txt <- c("Number of Points:"
           ,formatC(length(dist),big.mark=" ")
           ,"Normal distribution"
           ,eval(substitute(expression(paste(mu, ":" , c, "mm"),list(c=(formatC(fit$estimate[1], digits = 3,format = "f"))))))
           ,paste(expression(sigma),":",formatC(fit$estimate[2], digits = 3,format = "f"),"mm")
           ,"Density plot"
           ,paste(expression(bar(x)),":",formatC(mean, digits = 3,format = "f"),"mm")
           ,paste("SD:",formatC(sd, digits = 3,format = "f"),"mm")
           )digits = 3,format = "f"))))))

导致 坐标:坐标 (formatC($(fit,estimate)_1,3,f))

我有一个问题,经过 2 小时的研究,我无法使 mu 显示为希腊表达式,或者如果我这样做,则不会评估 formatC() ,而是像文字而不是给出格式化的数字。

使用了其他帖子中的所有提示,例如 bquote 等,但无法使其运行。

编辑了一些“更可运行”的示例。 这是无法运行的代码:

x <- seq(1,100,1)
y <- runif(100)
y2 <- x + y 

fit <- lm(y2~x)
plot(x,y2)
abline(fit, col = "red",lwd = 2)
mu2    <- formatC(fit$coefficients[1], digits = 3,format = "f")
sigma2 <- formatC(fit$coefficients[2], digits = 3,format = "f")
mean <- mean(y2)
sd <- sd(y2)
leg.txt <- c("Number of Points:"
             ,formatC(length(dist),big.mark=" ")
             ,"Normal distribution"
             ,bquote(mu ~ ":" ~ .(mu2) ~ "mm")
             ,bquote(sigma ~ ":" ~ .(sigma2) ~ "mm")
             ,"Density plot"
             ,paste(expression(bar(x)),":",formatC(mean, digits = 3,format = "f"),"mm")
             ,paste("SD:",formatC(sd, digits = 3,format = "f"),"mm")
)
legend("topleft",  leg.txt
       ,col= c("white","white","blue","white","white","red","white","white")
       ,bty = "n"
       ,lwd = c(2,2)  
)

当我第二次使用 bquote 时出现问题。

运行的是

x <- seq(1,100,1)
y <- runif(100)
y2 <- x + y 

fit <- lm(y2~x)
plot(x,y2)
abline(fit, col = "red",lwd = 2)
mu2    <- formatC(fit$coefficients[1], digits = 3,format = "f")
sigma2 <- formatC(fit$coefficients[2], digits = 3,format = "f")
mean <- mean(y2)
sd <- sd(y2)
leg.txt <- c("Number of Points:"
             ,formatC(length(dist),big.mark=" ")
             ,"Normal distribution"
             ,bquote(mu ~ ":" ~ .(mu2) ~ "mm")
             ,expression(paste(sigma,":",eval(formatC(fit$estimate[2], digits = 3,format = "f"))),"mm")
             ,"Density plot"
             ,paste(expression(bar(x)),":",formatC(mean, digits = 3,format = "f"),"mm")
             ,paste("SD:",formatC(sd, digits = 3,format = "f"),"mm")
)
legend("topleft",  leg.txt
       ,col= c("white","white","blue","white","white","red","white","white")
       ,bty = "n"
       ,lwd = c(2,2)  
)

我仍然认为 bquote 是正确的选择。在我的真实数据中,我正在拟合正态分布,但这应该不重要。

使用 bquote 两次得到的错误是: 缺少参数图例,没有默认值(从德语错误消息翻译而来,但似乎是正确的。图例对象没有得到 txt,尽管它在我的工作区中。

现在最终有效的解决方案是:

x <- seq(1,100,1)
y <- runif(100)
y2 <- x + y 

fit <- lm(y2~x)
plot(x,y2)
abline(fit, col = "red",lwd = 2)
mu2    <- formatC(fit$coefficients[1], digits = 3,format = "f")
sigma2 <- formatC(fit$coefficients[2], digits = 3,format = "f")
mean <- mean(y2)
sd <- sd(y2)
leg.txt <- c("Number of Points:"
             ,formatC(length(dist),big.mark=" ")
             ,"Normal distribution"
             ,as.expression(bquote(mu ~ ":" ~ .(mu2) ~ "mm"))
             ,as.expression(bquote(sigma ~ ":" ~ .(sigma2) ~ "mm"))
)
legend("topleft",  leg.txt
       ,col= c("white","white","blue","white","white","red","white","white")
       ,bty = "n"
       ,lwd = c(2,2)  
)

最佳答案

您可以使用 substitute 来包含“数学”变量和数值变量,如 ?plotmath 中所示。但这里也有一些意想不到的事情;图例的元素向量无法正确组合,除非其中之一是表达式,所以在这里我只是将您的纯文本行之一制作为表达式。

fit <- list(estimate=c(1.35456, 2.63454))
dist <- 1:10
mean <- 10.3456
sd <- 0.1566

leg.txt <- c(paste("Number of Points:",formatC(length(dist),big.mark=" "))
                   ,expression("Normal distribution")
                   ,substitute(paste(mu,": ",x," mm"), 
                        list(x=formatC(fit$estimate[1], digits = 3,format = "f")))
                   ,substitute(paste(sigma,": ",x," mm"), 
                        list(x=formatC(fit$estimate[2], digits = 3,format = "f")))
                   ,"Density plot"
                   ,substitute(paste(bar(x),": ",y," mm"), 
                        list(y=formatC(mean, digits = 3,format = "f")))
                   ,substitute(paste("SD: ",x," mm"), 
                        list(x=formatC(sd, digits = 3,format = "f")))
           )

plot(1:5,1:5)
legend("topleft",  legend=leg.txt
     ,col= c("white","blue","white","white","red","white","white")
     ,bty = "n"
     ,lwd = c(2,2)  
)

关于R - 如何为一个图例元素组合 expression()、paste() 和 formatC() 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21308576/

相关文章:

r - 如何在 R 中使用 mlr 保存水模型?

r - 为什么不应该在 R 包中使用 library() 或 require()

r - 如何获得R脚本的运行时?

r - 调试rmarkdown错误 'tlmgr'未找到

r - 在 optwrap 中使用 lmer 的模糊警告 lme4

r - 两个栅格之间的散点图,给出第三个栅格的颜色

r - 如何从data.frame获取 "code for creating a variable"

r - 使用拼凑法排列多个地 block 时,如何将图例合并为地 block 大小图?

r - SAS:如何将所有数据集变量包含到模型中

r - 使用 href 信息框作为操作按钮