2012-02-24 13 views
4

次のような大きなデータがありますが、これはほんの少しのサンプルです。水平線グラフと一致するR

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変数PosはY軸になり、X軸はX変数(量的)になります。各Pos値の水平バー長は、開始点と終了点によって定義されます。たとえば、1の行は1から始まり、x軸の3で終わります。

以下は、希望のFigure出力の概略図です。

enter image description here

+2

最後の例は役に立ちです...あなたの図のように、より正確にそれを取得するには? http://had.co.nz/ggplot2/geom_linerange.html –

+0

はい、確かに..有益です – jon

答えて

3

線を描画するためにgeom_segmentで使用パッケージggplot2ggplotのために、この必要なデータ構造は、data.frameにあなたのデータを組み合わせることにより、

スタート:

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) 
) 

は、プロットを作成します。ベースRで

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

4

.. 。

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

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) 
関連する問題