【问题标题】:Is there an elegant way to deal with log-transformed axes and units from the units package in R?有没有一种优雅的方法来处理 R 中的 units 包中的对数转换轴和单位?
【发布时间】: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


    【解决方案1】:

    您可以通过coord_trans 应用它,而不是在比例内应用对数转换。 (顺便说一句:在图中添加刻度标签)试试这个:

    library(units)
    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) +
      scale_y_unit() +
      coord_trans(y = "log10")
    

    由reprex package (v0.3.0) 于 2020-04-14 创建

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      相关资源
      最近更新 更多