r - how to print unit of the feature on plot using function? -
i have function creating plots in r. wish print unit along feature name. below code.
library(ggplot2) plotfunction <- function(feature, featur_name, feature_unit){ ggplot(aes(x = feature), data = mtcars) + geom_histogram(binwidth = 0.5) + labs(x = paste(featur_name, feature_unit), y = "count, #") } plotfunction(mtcars$wt, "displacement, ", expression({g}/{dm}^{3}))
the function produces plot as shown below.
not graceful solution, here goes:
plotfunction2 <- function(feature_name, feature_unit){ x.lab = paste0("expression(paste('", feature_name, "', ", feature_unit, "))") ggplot(data = mtcars, aes(wt)) + geom_histogram(binwidth = 0.5) + labs(x = eval(parse(text = x.lab))) } plotfunction2("displacement, ", "{g}/{dm}^{3}")
(i left first argument out because data input isn't part of real question here... though shouldn't define aesthetic mapping $
sign.)
Comments
Post a Comment