Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subscript and superscript in ylab of qplot [R]

Tags:

r

ggplot2

I tried using expression(), which works with noraml plots, but my attempt below fails: any ideas?

I want to write: µg CO2 (subscript 2) – C m-2 (superscript -2) h-1 (supercript -1)

works perfectly:

plot(CO2~water_content, data=gases, ylab = expression(paste("µg ", CO[2], " - C ", m^-2, " ", h^-1, sep="")))             

fails:

qplot(factor(vegetation_dummy),CO2,facets=sampling~biochar,geom=c('boxplot'),data=gases_PL)+theme_bw()+xlab('Plants')+ylab = expression(paste("µg ", CO[2], " - C ", m^-2, " ", h^-1, sep=""))

Many thanks!

like image 880
Michi Ka Avatar asked Mar 29 '12 10:03

Michi Ka


People also ask

How do you write subscript and superscript in R?

Subscripts and SuperscriptsTo indicate a subscript, use the underscore _ character. To indicate a superscript, use a single caret character ^ . Note: this can be confusing, because the R Markdown language delimits superscripts with two carets. In LaTeX equations, a single caret indicates the superscript.

How do I add a subscript in R plot?

You can use bquote when working with subscripted variables. Say, nIter <- 2 , then plot(1, 1, main = bquote(title[. (nIter)])) is exactly what you need (taken from the R-help mailing list).


1 Answers

your command fails, because the syntax is wrong

qplot(..) ... + ylab = expression(...) 

you need something like '+ ylab(...)'

for example like this:

data(diamonds)
qplot(carat, depth, data=diamonds, facets = cut~color, geom='boxplot') + 
ylab(expression(paste("µg ", CO[2], " - C ", m^-2, " ", h^-1, sep="")))
like image 77
smu Avatar answered Oct 26 '22 19:10

smu