单位库简化了单位的使用。据我所知,使用单位进行绘图可以与基本绘图一起使用,但不能与ggplot一起使用。有什么建议?
library(units)
# Take cars data frame: stopping dist (ft) vs speed (mph)
plot(cars)
# units + base plot
Distance = set_units(cars$dist, ft)
Speed = set_units(cars$speed, mph)
plot(x=Speed, y=Distance) #Clean
# units + ggplot
library(ggplot2)
df = cars
df$Disance = set_units(df$dist, ft)
df$Speed = set_units(df$speed, mph)
qplot(x=Speed, y=Distance, data=df)
# Error in Ops.units(x, range[1]) :
# both operands of the expression should be "units" objects
您可以使用ggforce
它来解决此问题。
更具体地说,请参见scale_unit
。
# install.packages("ggforce")
library(ggplot2)
library(ggforce)
df = cars
df$Distance = set_units(df$dist, ft)
df$Speed = set_units(df$speed, mph)
qplot(x=Speed, y=Distance, data=df)
这样您将获得相同的结果,以避免避免转换数据。
qplot(x=speed, y=dist, data=cars) +
scale_x_unit(unit = 'mph') +
scale_y_unit(unit = 'ft')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句