开发者

Using subscript and variable values at the same time in Axis titles in R

开发者 https://www.devze.com 2023-03-27 02:13 出处:网络
I want to use the title \"CO2 emissions in wetlands\" in a plot in R, whereas th开发者_StackOverflow中文版e 2 in CO2 is in subscript, and the value for the region (here: \"wetlands\") is contained in

I want to use the title "CO2 emissions in wetlands" in a plot in R, whereas th开发者_StackOverflow中文版e 2 in CO2 is in subscript, and the value for the region (here: "wetlands") is contained in a variable named "region".

region = "wetlands"
plot (1, 1, main=expression(CO[2]~paste(" emissions in ", region)))

The problem is, that not the value of the variable is pasted, but the name of the variable. This gives "CO2 emissions in region" instead of "CO2 emissions in wetlands". I also tried:

region="wetlands"
plot (1,1,main=paste(expression(CO[2]), "emissions in", region))

But the subscript is not done here, and the title is: "CO[2] emissions in wetlands".

Is it somehow possible to get values of variables into expression?

Thanks for your help,

Sven


There is no need to use paste() in producing an expression for plothmath-style annotation. This works just fine:

region <- "foo"
plot (1, 1, main = bquote(CO[2] ~ "emissions in" ~ .(region)))

giving:

Using subscript and variable values at the same time in Axis titles in R

Using paste() just gets in the way.

Nb: You have to quote "in" because the parser grabs it as a key part of R syntax otherwise.


You can use substitute:

mn <- substitute(CO[2]~ "emissions in" ~ region, list(region="wetlands") )
plot(1, 1, main=mn )

Using subscript and variable values at the same time in Axis titles in R

From the ?substitute help file:

The typical use of substitute is to create informative labels for data sets and plots. The myplot example below shows a simple use of this facility. It uses the functions deparse and substitute to create labels for a plot which are character string versions of the actual arguments to the function myplot.


For your case, stolen from one of the answers at the duplicated link:

x <- "OberKrain"
plot(1:10, 1:10, main = bquote(paste(CO[2], " in ", .(x))))

Using subscript and variable values at the same time in Axis titles in R

0

精彩评论

暂无评论...
验证码 换一张
取 消