Combining paste () and expression () functions in graph labels

Consider this simple example:

labNames <- c('xLab','yLabl') plot(c(1:10),xlab=expression(paste(labName[1], x^2)),ylab=expression(paste(labName[2], y^2))) 

I want the character entry to be defined by the variable labName, 'xLab' or 'yLab', so that it appears next to X ^ 2 or y ^ 2 defined by the expression (). Be that as it may, the actual text labName with index is appended to the superscript.

Any thoughts?

+59
r plot
Feb 11 2018-11-11T00:
source share
5 answers

An alternative solution for @Aaron is the bquote() function. We need to provide a valid R expression, in this case LABEL ~ x^2 , for example, where LABEL is the string that you want to assign from the vector labNames . bquote evaluates the R code in the expression enclosed in .( ) , and returns the result in the expression.

Here is an example:

 labNames <- c('xLab','yLab') xlab <- bquote(.(labNames[1]) ~ x^2) ylab <- bquote(.(labNames[2]) ~ y^2) plot(c(1:10), xlab = xlab, ylab = ylab) 

(Note that ~ just adds a little space, if you don't need space, replace it with * , and the two parts of the expression will be matched.)

+59
Feb 14 2018-11-11T00:
source share

Use substitute instead.

 labNames <- c('xLab','yLab') plot(c(1:10), xlab=substitute(paste(nn, x^2), list(nn=labNames[1])), ylab=substitute(paste(nn, y^2), list(nn=labNames[2]))) 
+25
Feb 11 '11 at 22:00
source share

EDIT: added a new example for ggplot2 at the end

Have a look ? Plotmath for various mathematical operations in R

You should be able to use an expression without insertion. If you use the tilde character (~) in an expression function, it will assume that there is a space between the characters, or you can use the * character and it will not put a space between the arguments

Sometimes you will need to change the margins when you place superscripts on the y axis.

 par(mar=c(5, 4.3, 4, 2) + 0.1) plot(1:10, xlab = expression(xLab ~ x^2 ~ m^-2), ylab = expression(yLab ~ y^2 ~ m^-2), main="Plot 1") 

enter image description here

 plot(1:10, xlab = expression(xLab * x^2 * m^-2), ylab = expression(yLab * y^2 * m^-2), main="Plot 2") 

enter image description here

 plot(1:10, xlab = expression(xLab ~ x^2 * m^-2), ylab = expression(yLab ~ y^2 * m^-2), main="Plot 3") 

enter image description here

We hope that you can see the differences between charts 1, 2 and 3 with different uses of the symbols ~ and *. Additional note, you can use other symbols, such as drawing a degree symbol for temperature for or mu, fi. If you want to add an index, use square brackets.

 plot(1:10, xlab = expression('Your x label' ~ mu[3] * phi), ylab = expression("Temperature (" * degree * C *")")) 

enter image description here

Here is a ggplot example using an expression with a meaningless example

 require(ggplot2) 

Or, if you have the pacman library installed, you can use p_load to automatically download, download, and attach additional packages.

 # require(pacman) # p_load(ggplot2) data = data.frame(x = 1:10, y = 1:10) ggplot(data, aes(x,y)) + geom_point() + xlab(expression(bar(yourUnits) ~ g ~ m^-2 ~ OR ~ integral(f(x)*dx, a,b))) + ylab(expression("Biomass (g per" ~ m^3 *")")) + theme_bw() 

enter image description here

+19
Sep 08 '15 at 19:21
source share

A very good example of using insertion and replacement for a set of both characters (mathplot) and variables at http://vis.supstat.com/2013/04/matumatic-annotation-in-r/

Here is the ggplot adaptation

 library(ggplot2) x_mean <- 1.5 x_sd <- 1.2 N <- 500 n <- ggplot(data.frame(x <- rnorm(N, x_mean, x_sd)),aes(x=x)) + geom_bar() + stat_bin() + labs(title=substitute(paste( "Histogram of random data with ", mu,"=",m,", ", sigma^2,"=",s2,", ", "draws = ", numdraws,", ", bar(x),"=",xbar,", ", s^2,"=",sde), list(m=x_mean,xbar=mean(x),s2=x_sd^2,sde=var(x),numdraws=N))) print(n) 
+7
Nov 20 '14 at 17:52
source share

If x ^ 2 and y ^ 2 were expressions already given in the squared variable, this solves the problem:

 labNames <- c('xLab','yLab') squared <- c(expression('x'^2), expression('y'^2)) xlab <- eval(bquote(expression(.(labNames[1]) ~ .(squared[1][[1]])))) ylab <- eval(bquote(expression(.(labNames[2]) ~ .(squared[2][[1]])))) plot(c(1:10), xlab = xlab, ylab = ylab) 

Pay attention to [[1]] per square [1]. It gives you the contents of an "expression (...)" between brackets without any escape characters.

+3
Nov 17 '15 at 13:27
source share



All Articles