2

I'm writing a wrapper function around ggplot2 and having difficulty with one of the string arguments passed. Here's the sample code

myPlot <- function(tim, labx){
  ggplot(subset(dat,TIME=tim), aes(x=WT, y=Var))+
    geom_point(size=2)+
    facet_wrap(~Dose)+
    scale_x-continuous(expression(bold("Predicted"~labx~"Concentration ("*mu*"g/mL)")))
}

When I say myplot(100, "Week3"), my x-axis label is showing as "Predicted labx Concentration (µg/mL)" instead of "Predicted Week3 Concentration (µg/mL)". How do I fix this?

1

1 Answer 1

1

One solution is to use bquote() instead of expression(), and use .() inside of bquote to evaluate character (string) variables.

Below is a fully reproducible example of the issue.

library(ggplot2)

labx = "Week3"

p = ggplot(data.frame(x=1:5, y=1:5), aes(x, y)) + 
    geom_point() + 
    xlab(bquote(bold(Predicted~.(labx)~Concentration~(mu*g/mL))))
p

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. Worked like a charm !!!. Thanks so much. I was struggling for so long to solve this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.