I'm trying to construct a general function to generate some graphics. So I have to create an axis label that combines an expression and a string. I've tried:
i <- 2
measure <- c(expression((kg/m^2)), "(%)")
variable <- c("BMI", "Cintilografia 1h")
data <- data.frame(x = 0, y = 0)
gp <- ggplot(data, aes(x = x, y = y)) +
geom_point() +
labs(y = parse(text = paste(variable[i], measure[i])))
It works for i = 1
, but for i = 2
.
I discovered parse function has some problems with special characters. I would prefer that the user-function does not to worry about these features. Then, I'm looking for a more general solution.
Does anyone have any idea? Thanks in advance