【问题标题】:How to break axis in a barplot (maybe using plotrix gap.barplot)?如何在条形图中打破轴(可能使用 plotrix gap.barplot)?
【发布时间】:2020-05-17 04:46:30
【问题描述】:

我发现很多 SO 问题和答案都解决了轴上的中断和间隙。但是由于没有示例代码,没有图片或复杂的代码,它们中的大多数都是低质量的(在 SO 的意义上)。这就是我问的原因。

我尝试使用library(plotrix)。如果有没有它和/或另一个库的解决方案,我也可以。

这是一个正常的 R-barplot。

barplot(c(10,20,500))

为了打破轴并增加间隙,我尝试了这个。

gap.barplot(c(10,20,500),gap=c(50,400), col=FALSE)

结果不漂亮

  • 条之间没有空间。来自barplot()space 参数不被gap.barplot() 接受。
  • 条形有不同的宽度。
  • 抽动的位置不在条的中间。

我可以用plotrix 控制这些参数吗?我在文档中没有看到有关它的内容。 是否有其他库或解决方案可以解决我的问题?

【问题讨论】:

    标签: r bar-chart axis


    【解决方案1】:

    由于很多个人问题,有很多不同的答案。对于您的问题,您可以尝试以下方法。但总有更好的解决方案。而且 IMO 总是更好地显示您的完整数据而不是裁剪它。

    # Your data with names
    library(plotrix)
    d <- c(10,20,500)
    names(d) <- letters[1:3]
    # Specify a cutoff where the y.axis should be splitted.
    co <- 200
    # Now cut off this area in your data.
    d[d > co] <- d[d > co] - co
    # Create new axis label using the pretty() function
    newy <- pretty(d)
    newy[ newy > co] <- newy[ newy > co] + co
    # remove values in your cutoff. 
    gr <- which(newy != co)
    newy <- newy[ gr ]
    # plot the data
    barplot(d, axes=F)
    # add the axis
    axis(2, at = pretty(d)[gr], labels = newy)
    axis.break(2, co, style = "gap") 
    

    作为替代方案,您可以尝试使用log="y" 记录您的轴。

    【讨论】:

    • “而且 IMO 总是更好地显示您的完整数据而不是裁剪它。” - 同意!
    • 请看#9这一行。你确定这是正确的吗?
    • @buhtz 我修好了。谢谢。
    • 非常好的代码。很容易理解那里发生了什么。太感谢了!但这是一个技巧/解决方法。所以似乎没有 R 解决方案,因为 R 人不想削减数据? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2015-05-02
    • 2019-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多