2013-04-01 92 views
32

我正在嘗試使用特殊字符和上標來製作y軸標題。我能夠做到這一點,但我希望左括號不能被上標。這就是我遇到的問題。我認爲它只是放置我的括號,但我已經試過(看似)一切。情節軸標題上的特殊字符和上標

plot(WatexCl, ConcuM, col = as.numeric(1), pch = as.numeric(Depth), 
    xlab = expression(paste("Concentration Cl (", mu, "moles/g dry wt)")), 
    ylab = expression(paste("Average Conc of S- on plates (", mu, "Moles/cm"^"2"),)), 
    data = plates) 

回答

55

一兩件事,往往用戶無法把握的是,你總是不需要引用字符串和paste它們在一起用於劇情標籤的表達式時。直接使用佈局工具通常更簡單(例如,~*)。例如:

df <- data.frame(y = rnorm(100), x = rnorm(100)) 

plot(y ~ x, data = df, 
    ylab = expression(Average ~ Conc ~ of ~ S- ~ on ~ plates ~ 
         (mu ~ Moles ~ cm^{-2} ~ dry ~ wt)), 
    xlab = expression(Concentration ~ Cl ~ (mu ~ moles ~ g^{-1} ~ dry ~ wt))) 

或者,您可以包含更長的文本部分的字符串;在這種情況下,它可以說是更容易做到:

plot(y ~ x, data = df, 
    ylab = expression("Average Conc of S- on plates" ~ 
         (mu ~ moles ~ cm^{-2} ~ "dry wt")), 
    xlab = expression("Concentration Cl" ~ (mu ~ moles ~ g^{-1} ~ "dry wt"))) 

但要注意有沒有必要paste字符串和其他功能在這裏。

兩個生產:

enter image description here

注意的問題plotmath與上標2.您不妨添加一些額外的空間,爲y軸的保證金,以適應:

op <- par(mar = c(5,4.5,4,2) + 0.1) 
plot(y ~ x, data = df, 
    ylab = expression("Average Conc of S- on plates" ~ 
          (mu ~ moles ~ cm^{-2} ~ "dry wt")), 
    xlab = expression("Concentration Cl" ~ (mu ~ moles ~ g^{-1} ~ "dry wt"))) 
par(op) 

生產

enter image description here

5

這解決了超級腳本右括號的問題:

# reproducible data 
plates <- data.frame(WatexCl = rnorm(100), ConcuM = rnorm(100), Depth = rnorm(100)) 

# alter the default plot margins so the 
# superscript in the y-axis label is completely displayed 
par(mar=c(5,5,4,2)) 

# draw the plot 
plot(WatexCl ~ ConcuM, data = plates, 
    col = as.numeric(1), 
    pch = as.numeric(Depth), 
    xlab = bquote("Concentration Cl ("*mu~"moles/g dry wt)"), 
    ylab = bquote("Average Conc of S- on plates ("~mu~"Moles/cm"^"2"*")")) 

enter image description here

+2

'bquote'在這裏有點矯枉過正。 '表達式'應該可以正常工作。當需要用表達式中的值替換表達式中的對象時,'bquote'是最有用的。 –

+0

@GavinSimpson,謝謝,很高興知道!看起來'表達式'在間距字符上的效果比'bquote'更好,我的情節上的圓括號附近有幾個奇數空格...... – Ben

+5

這是因爲你用'〜mu〜'來分隔兩個引用的字符串。如果你把它設置成'* mu〜',你會得到正確的間距。 '〜'是一個間距運算符,'*'並置這些參數(即沒有空格)。 –