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

有帮助吗?

解决方案

Use package ggplot2 with geom_segment to draw the lines.

Start by combining your data into a data.frame, since this the required 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 the plot:

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

其他提示

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)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top