在R中打破X轴

时间:2013-10-26 21:56:17

标签: r plotrix

我想在我的情节中得到一个破X轴。在x轴上,我喜欢插入一个断轴符号< // > [从2开始并在结束8结束,这意味着2-8将隐藏在< // >符号中]所以其他值可以是强调。在Matlab中,此任务使用BreakXAxis执行。在R中, plotrix 库只能插入一个断轴符号,这就是全部。

x <- c(9.45, 8.78, 0.93, 0.47, 0.24, 0.12)
y <- c(10.72, 10.56, 10.35, 10.10, 9.13, 6.72)
z <- c(7.578, 7.456, 6.956, 6.712, 4.832, 3.345)
plot(x, y, col='blue', pch=16, xlab= 'x', ylab='y, z')
points(x, z, col='red', pch=17)
library(plotrix)
axis.break(1,2,style="slash") 

2 个答案:

答案 0 :(得分:15)

听起来你需要gap.plot

library(plotrix)
par(bty="n") # deleting the box
gap.plot(x,y, gap=c(2,7.5), gap.axis="x", pch=16,
         col="blue", ylim=range(c(y,z)),
         xtics=c(0:3,8:10), xticlab=c(0:3,8:10))

gap.plot(x,z, gap=c(2,7.5), gap.axis="x", pch=17,
         col="red", ylim=range(c(y,z)), add=TRUE); axis(2)

abline(v=seq(1.99,2.09,.001), col="white")  # hiding vertical lines
axis.break(1,2,style="slash")               # plotting slashes for breakpoints

enter image description here

答案 1 :(得分:14)

xgap <- ifelse(x > 8, x-6, x)
#Possibly you'd want to check if there are values between 2 and 8.
plot(xgap, y, col='blue', pch=16, xlab= 'x', ylab='y, z', xaxt="n")
points(xgap, z, col='red', pch=17)
xat <- pretty(xgap)
xat <- xat[xat!=2]
xlab <- ifelse(xat>2, xat+6, xat)
axis(1,at=xat, labels=xlab)
library(plotrix)
axis.break(1,2,style="slash") 

enter image description here

不要这样做。 gap.plot提供了一个更好的选择,但我可能会使用facet,例如ggplot2。