如何使用函数在图上打印特征的单位?

时间:2017-09-12 02:56:51

标签: r function plot ggplot2

我有一个在R中创建绘图的功能。我希望打印单元和功能名称。以下是代码。

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}))

该函数生成如下所示的图。

enter image description here

1 个答案:

答案 0 :(得分:0)

不是最优雅的解决方案,但这里有:

{
    "keys": ["super+s"],
    "command": "toggle_white_space",
    "args": {
        "options": ["all", "none"]
    }
},

plot

(我离开了第一个参数,因为数据输入不是真正问题的一部分......虽然您可能不应该用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}") 符号定义美学映射。)