How to break axis in a barplot (maybe using plotrix gap.barplot)?

霸气de小男生 提交于 2021-01-29 08:31:37

问题


I found a lot of SO question and answers addressing break and gaps in axis. But most of them are of low quality (in an SO meaning) because of no example code, no picture or to complex codes. This is why I asking.

I try to use library(plotrix). If there is a solution without it and/or another library it would be ok for me, too.

This is a normal R-barplot.

barplot(c(10,20,500))

To break the axis and add gap I tried this.

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

The result is not beautiful.

  • There is no space between the bars. space parameter from barplot() is not accepted by gap.barplot().
  • The bars have different widths.
  • The position of the tics are not in the middle of the bar.

Can I control that parameters with plotrix? I don't see something about it in the documentation. Is there another library or solution for my problem?


回答1:


There are so many different answers because of a lot of individual problems. For your problem you can try the following. But there is always a better solution out there. And IMO its always better to show your complete data instead of cropping it.

# 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") 

As an alternative you can try to log your axis using log="y".



来源:https://stackoverflow.com/questions/60005173/bar-plot-with-y-axis-break

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!