Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

horizontal line graph with assambly in R

I have big data like the following but this just little sample.

pos <- c(1, 3, 5, 8, 10, 12)
start <- c(1,3, 6, 7, 10, 11)
end <- c(5, 6, 9, 9, 13, 12)

Qunatative variable Pos will be Y axis and X axis will be anthor X variable (quantitative). The horizontal bar length for each Pos value is defined by start and end point. For example,line for 1 will start from 1 and end at 3 in x axis.

The following is rough sketch of desired the figure output.

enter image description here

like image 223
jon Avatar asked Feb 22 '23 08:02

jon


1 Answers

In base R...

plot(pos, type = 'n', xlim = range(c(start, end)), ylim = c(13,0))
grid()
segments(start, pos, end, pos)

To get it more exactly like your figure...

r <- par('usr') 
plot(pos, type = 'n', xlim = range(c(start, end)), ylim = c(13.5,0.5), xlab = '', 
    xaxt = 'n', yaxt = 'n', panel.first = rect(r[1], r[3], r[2], r[4], col = 'goldenrod'))
# abline(h = 1:13, col = 'white')
# abline(v = 1:13, col = 'white')
grid(lty = 1, col = 'white')
axis(1, 1:13, 1:13, cex.axis = 0.8)
axis(2, 1:13, 1:13, las = 1, cex.axis = 0.8)
segments(start, pos + 0.5, end, pos + 0.5, lwd = 2)
like image 68
John Avatar answered Mar 04 '23 18:03

John