Planning to draw a density line-plot with gapped (or broken) Y-axis in R, I initially tried out the plotrix package (version 3.8.1). 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.

packageVersion("plotrix")
#[1] '3.8.1'
library(plotrix)

#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(gapDist/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,gapDist/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)





0

Add a comment

In this post I show how groupScatterPlot(), function of the rnatoolbox R package can be used for plotting the individual values in several groups toge

In this post I show how classifySex(), function of the rnatoolbox R package can be used for inferring the sex of  the studied subjects from their bina

2
Recently I have started to organize my commonly used functions related to quality assessment and analyzing RNAseq data into an R package.

Many times, in our projects, we may need to compare different measured factors in our samples to one another, and study whether they are linearly depe

2
Many times, in our projects, we may need to compare different measured factors in our samples to one another, and study whether they are linearly depe
Many times, in our projects, we may need to compare different measured factors in our samples to one another, and study whether they are linearly depe
Many times, in our projects, we may need to compare different measured factors in our samples to one another, and study whether they are linearly depe
Occasionally when indexing data frames the format is converted, leading to confusing consequences.

Example of a fastq file in read 1 (in paired read sequencing) is as follows:

@SRR3117565.1.1 1 length=100

NCAAAACAGCTCTCCCTCCTTTGATCTGATGGTCTGCAGAGG

Yesterday (on the opening day of the new Batman movie) I search the Internet for the Batman formula and it's implementations in R.

Yesterday (on the opening day of the new Batman movie) I search the Internet for the Batman formula and it's implementations in R.
Labels
Blog Archive
About Me
About Me
My Photo
I am a Postdoc researcher at the Neuromuscular Disorders Research lab and Genetic Determinants of Osteoporosis Research lab, in University of Helsinki and Folkhälsan RC. I specialize in Bioinformatics. I am interested in Machine learning and multi-omics data analysis. My go-to programming language is R.
My Blog List
My Blog List
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.