如何在一个条形图中打破轴(也许使用plotrix gap.barplot)?

时间:2016-10-19 10:35:17

标签: r bar-chart axis

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

我尝试使用library(plotrix)。如果有没有它的解决方案和/或其他库,那对我来说也没问题。

这是一个正常的R-barplot。

barplot(c(10,20,500))

barplot with standard R

为了打破轴并增加差距我试过了。

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

barplot using plotrix library

结果不漂亮

  • 酒吧之间没有空间。 space
  • 不接受来自barplot()的{​​{1}}参数。
  • 条形宽度不同。
  • 抽搐的位置不在栏的中间。

我可以使用gap.barplot()控制参数吗?我在文档中没有看到它。 我的问题是否有另一个库或解决方案?

1 个答案:

答案 0 :(得分: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") 

enter image description here 您也可以尝试使用log="y"来记录您的轴。