【发布时间】:2020-07-27 10:47:37
【问题描述】:
units 包对于处理单位非常有用。但是,我不清楚如何在对数转换轴上使用单位绘制变量。在基本图中,添加log="y" 参数会导致错误。使用标准 scale_y_log10() 函数的 ggplot2 也是如此。或者,使用 ggforce 函数scale_y_unit(trans="log10") 可以工作,但会产生一个神秘的轴标签。
- 有没有办法在基础情节中进行这项工作?
- 有没有办法让scale_y_unit(trans="log10") 不会弄乱轴标签,而是专注于轴上的数字和数据?
library(units)
# Take cars data frame: stopping dist (ft) vs speed (mph)
plot(cars)
# Using base plot
Distance = set_units(cars$dist, ft)
Speed = set_units(cars$speed, mph)
plot(x=Speed, y=Distance, log="y")
# Error in Ops.units(y, 0) :
# both operands of the expression should be "units" objects
# Using ggplot
library(ggplot2)
library(ggforce)
df = cars
df$Disance = set_units(df$dist, ft)
df$Speed = set_units(df$speed, mph)
qplot(x=Speed, y=Distance, data=df) +
scale_y_unit(trans="log10")
# Y-axis is cryptically labelled with "Distance (lg(re 0.3048 m))"
【问题讨论】:
标签: r ggplot2 plot units-of-measurement