我正在绘制使用避风港语义标记的数据,即变量和值具有通过属性定义的标签。
通常,这些标签也是我在轴标题和刻度中想要的。
library(ggplot2)
mtcars$mpg = haven::labelled(mtcars$mpg, labels = c("low" = 10, "high" = 30))
attributes(mtcars$mpg)$label = "miles per gallon"
ggplot(mtcars, aes(mpg, cyl)) + geom_point() +
scale_x_continuous(attributes(mtcars$mpg)$label,
breaks = attributes(mtcars$mpg)$labels,
labels = names(attributes(mtcars$mpg)$labels))
我可以编写一个助手,用更容易迭代的东西替换那个费力的 scale_x_continuous 语句吗?例如类似的东西
scale_x_continuous(label_from_attr, breaks = breaks_from_attr, labels = value_labels_from_attr)
。或者甚至+ add_labels_from_attributes()
可以替换整个东西?
我知道我可以编写/使用助手Hmisc::label
来稍微缩短上面的属性代码,但这不是我想要的。