Horizontal line graph with supposedly in R

I have big data like the following, but this is just a small example.

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

The punitive variable Pos will be the Y axis, and the X axis will be the anthor X (quantitative) variable. The horizontal stripe length for each Pos value is determined by the start and end points. For example, the line for 1 starts at 1 and ends at 3 on the x axis.

Below is an approximate sketch of the desired result.

enter image description here

+4
source share
2 answers

Use ggplot2 with geom_segment to draw lines.

Start by combining your data into data.frame , as this is the necessary data structure for ggplot :

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

Create a schedule:

 library(ggplot2) ggplot(dat) + geom_segment(aes(x=start, y=pos, xend=end, yend=pos), color="blue", size=3) + scale_y_reverse() 

enter image description here

+3
source

At the base of R ...

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

To get it more accurately, 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) 
+5
source

Source: https://habr.com/ru/post/1398236/


All Articles