如何在条形图中断开轴(也许使用plottrix gap.barplot)? [英] How to break axis in a barplot (maybe using plotrix gap.barplot)?

查看:40
本文介绍了如何在条形图中断开轴(也许使用plottrix gap.barplot)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是一个普通的R条形图.

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)

结果是不好看.

  • 两个条之间没有空格. gap.barplot()不接受 barplot()中的 space 参数.
  • 条形的宽度不同.
  • 抽芯的位置不在条形的中间.

我可以使用 plotrix 控制该参数吗?我没有在文档中看到任何有关它的信息.我的问题还有其他图书馆或解决方案吗?

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?

推荐答案

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

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

或者,您可以尝试使用 log ="y" 记录轴.

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

这篇关于如何在条形图中断开轴(也许使用plottrix gap.barplot)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆