2012-04-14 112 views
67

我無法找到如何在R中的標題或小標題中編寫下標的方法。 如何將1,2和1,2作爲下標來書寫?R中的下標腳本

感謝您的幫助!

回答

99

expression是你的朋友?

plot(1,1, main=expression('title'^2)) #superscript 
plot(1,1, main=expression('title'[2])) #subscript 
+1

如何從變量中加載「2」?我有一個循環,需要繪製x_ [1] x_ [2] x_ [3] ... – 0x2207 2014-12-11 11:38:42

+3

您可以在處理下標變量時使用'bquote'。比如說,'nIter < - 2',然後'plot(1,1,main = bquote(title [。(nIter)]))'就是你需要的東西(來自[R-help郵件列表] //stat.ethz.ch/pipermail/r-help/2005-May/070670.html))。 – fdetsch 2015-01-07 09:55:14

23

見表達

plot(1:10,main=expression("This is a subscript "[2])) 

enter image description here

+2

?plotmath實際上可能對此更有幫助...... – Dason 2012-04-14 19:17:58

85

如果你正在尋找有多個標在一個文本,然後使用星號(*),以分開部分:

plot(1:10, xlab=expression('hi'[5]*'there'[6]^8*'you'[2])) 
+4

哇,這是一個爲期兩年的老問題的答案,原來是我所需要的。謝謝! – Michael 2014-05-24 00:25:53

+2

如果你想下標是一個字符串,只需將它放在引號中: 'plot(1:10,xlab = expression('hi'[5] *'there'[6]^8 *'you' '''''''''''')')' – smacdonald 2014-09-11 07:28:29

+1

這是一個很好的提示,因爲您可以在下標中放入[[數字]或[[字符]或甚至是[a5]]但不是'[5a]'或'[aa]'。我最近發現:'expression('x'['10sdt'])' – Cyrille 2014-09-19 13:26:02

5

下標並參考儲值...

a <- 10 
plot(c(0,1), c(0,1), type = 'n', ann = FALSE, xaxt = 'n', yaxt = 'n') 
text(0.2, 0.6, cex = 1.5, bquote(paste('S'['f']*' = ', .(a)))) 

enter image description here

0

再如,表達對工作負上標,而不需要圍繞負數報價:

title(xlab=expression("Nitrate Loading in kg ha"^-1*"yr"^-1)) 

,你只需要*來分隔上面提到的部分(當你寫一個上標或下標,並且需要在表達式之後添加更多的文本)。