Can't write the subscript in expression in R

↘锁芯ラ 提交于 2020-01-07 04:57:07

问题


I'm trying to create a ylab for my plot in m/s^2.

plot(mydata[,1], mydata[,3], type  = "l", xlab="Time [s]", ylab=expression("Acceleration [m/s"^{2}"]"), 
     main="Example of signal of horizontal acceleration", cex.main = 0.9)

The problem is after the ^ symbol ] gets superscripted.

with my code I get this error,

> source('~/Dropbox/WORK/ISRProject/R_projects/plots_report.R')
Error in source("~/Dropbox/WORK/ISRProject/R_projects/plots_report.R") : 
  ~/Dropbox/WORK/ISRProject/R_projects/plots_report.R:26:99: unexpected ']'
25: 
26: plot(mydata[,1], mydata[,3], type  = "l", xlab="Time [s]", ylab=expression("Acceleration [m/s"^{2}"]"
                                                                                                     ^

回答1:


Try: ylab=expression(paste("Acceleration [ ",m/s^{2}," ]"))

par(mar=c(4,5,4,4))
x <- seq(1,10,.01)
plot(x, sin(pi/2*x), type  = "l", xlab="Time [s]", 
     ylab=expression(paste("Acceleration [ ",m/s^{2}," ]")), 
     main="Example of signal of horizontal acceleration", cex.main = 0.9)



来源:https://stackoverflow.com/questions/22487229/cant-write-the-subscript-in-expression-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!