Axis break in R for line plot

[This article was first published on gacatag, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Planning to draw a density line-plot with gapped (or broken) Y-axis in R, I initially tried out the plotrix package. However after facing a couple of problems, I ended up using the standard R graphics codes to draw the correct gapped line-plot.

Here I will only apply the break to the y-axis of a density line-plot, however similar method can be used for any line-plot or their X-axis as well.

#Defining values
n <- rnorm(100000, mean = 100, sd = 36)
m <- rnorm(500000, mean = 150, sd = 20)
p <- rnorm(1000000, mean = 160, sd = 20)
nd<- density(n)
md<- density(m)
pd<- density(p)

#Normal plotting
lwd<-1
plot(md, col=”red”, xlim=c(0,250))
points(nd,  type=”l”)
points(pd, col=”blue”, type=”l”)
legend(“topleft”, legend=c(“n”, “m”, “p”), col=c(“black”, “red”, “blue”), lwd=lwd)





Here is how the gapped line plot would look using plotrix. As you can see since the values higher than the gap were taken out from the bottom window a horizontal line is drawn at the top of the bottom window ! I tried various parameter settings but didn’t succeed in removing the horizontal line.

# define gap
gap=c(0.013,0.017)
# Define line width
lwd<-1

# Plot all lines with the gap setting
gap.plot(x=md$x, y=md$y, type=”l”, gap=gap,col=”red”,main=”lineplot with gap”, add=F, xlim=c(0,250))
gap.plot(nd$x,nd$y, type=”l”, gap=gap,col=”black”,main=”lineplot with gap”, add=T)
gap.plot(pd$x,pd$y, type=”l”, gap=gap,col=”blue”,main=”lineplot with gap”, add=T)
gap.plot(md$x,md$y, type=”l”, gap=gap,col=”red”,main=”lineplot with gap”, add=T)
gap.plot(pd$x,pd$y, type=”l”, gap=gap,col=”blue”,main=”lineplot with gap”, add=T)
# Add legend
legend(“topleft”, legend=c(“n”, “m”, “p”), col=c(“black”, “red”, “blue”), lwd=lwd)
# Add “?!!!” mark
text(190,0.0125,”?!!!”, col=”red”, cex=2)




In the end I used these codes to correctly draw the gapped line-plot.

# Define gap
gap=c(0.013,0.017)
#Defien distance between the bottom and top windows 
gapDist<- .14
# Define line width
lwd<-1
# Define top vs bottom window size ratios (i.e. 1 vs 3)
layout(matrix(c(1,2), 2, 1, byrow = TRUE),
   heights=c(1,3))

# Define margin sizes of the top widow
    par(mar=c(yDist/2, 5.1, 4.1, 2.1))

# Plot all lines in the top window
    plot(md, bgcol=”white”, col=”transparent”, type=’l’, xlab=””, ylab=””,
        lty=1, main=””, xaxt=’n’, xlim=c(0,250), ylim=c(gap[2], max(md$y, na.rm=T)))
    points(nd, col=”black”, lwd=lwd, type=”l”)
    points(md, col=”red”, lwd=lwd, type=”l”)
    points(pd, col=”blue”, lwd=lwd, type=”l”)

# You can add legend to top window if you wish!
#legend(“topleft”, legend=c(“n”, “m”, “p”), col=c(“black”, “red”, “blue”), lwd=lwd)



# Define margin sizes of the bottom widow
    par(mar=c(5.1, 5.1,yDist/2, 2.1))
# Plot all in lines in the bottom window
    plot(md, col=”transparent”,
        xlim=c(0,250), ylim=c(min(md$y, na.rm=T), gap[1]),  type=’l’,
        ylab=”Density”, main=””)
    points(nd, col=”black”, lwd=lwd, type=”l”)
    points(md, col=”red”, lwd=lwd, type=”l”)
    points(pd, col=”blue”, lwd=lwd, type=”l”)

# Add legend to the top left of the bottom  window
# Alternatively you can add legend to top window! 
legend(“topleft”, legend=c(“n”, “m”, “p”), col=c(“black”, “red”, “blue”), lwd=lwd)





To leave a comment for the author, please follow the link and comment on their blog: gacatag.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)