Use expression with a variable r

后端 未结 4 1222
我寻月下人不归
我寻月下人不归 2020-12-13 06:23

I am trying to label a plot with the following label:

\"Some Assay EC50 (uM)\" where the \"u\" is a micro symbol.

I currently have:

assay &l         


        
相关标签:
4条回答
  • 2020-12-13 07:11

    You want a combination of bquote() and a bit of plotmath fu:

    assay <- "Some Assay"
    xlab <- bquote(.(assay) ~ AC50 ~ (mu*M))
    plot(0, xlab = xlab)
    

    The ~ is a spacing operator and * means juxtapose the contents to the left and right of the operator. In bquote(), anything wrapped in .( ) will be looked up and replaced with the value of the named object; so .(assay) will be replaced in the expression with Some Assay.

    0 讨论(0)
  • 2020-12-13 07:16

    You also could try the poor man's approach:

    assay <- "Some Assay"
    plot(0, xlab = paste0(assay, " AC50 (µM)"))
    

    It specifies the mu character directly rather than using expressions (and paste0 is just paste with sep = "").

    0 讨论(0)
  • 2020-12-13 07:18

    Using tidy_eval approach you could do

    library(rlang)
    
    assay <- "Some Assay"
    plot(0,xlab=expr(paste(!!assay," AC50 (",mu,"M)",sep="")))
    

    expr and !! are included in tidyverse, so you don't actually need to load rlang. I just put it there to be explicit about where they come from.

    0 讨论(0)
  • 2020-12-13 07:19

    another option using mtext and bquote

    plot(0,xlab='')
    Lines <- list(bquote(paste(assay," AC50 (",mu,"M)",sep="")))
    mtext(do.call(expression, Lines),side=1,line=3)
    

    Note that I set the xlab to null in the first plot.

    EDIT No need to call expression, since bquote will create an expression with replacement of elements wrapped in .( ) by their value. So a goodanswer is :

    plot(0,xlab='')
    Lines <- bquote(paste(.(assay)," AC50 (",mu,"M)",sep=""))
    mtext(Lines,side=1,line=3)
    
    0 讨论(0)
提交回复
热议问题