Ho grandi dati come il seguente, ma questo solo un piccolo campione.
pos <- c(1, 3, 5, 8, 10, 12)
start <- c(1,3, 6, 7, 10, 11)
end <- c(5, 6, 9, 9, 13, 12)
La variabile qunatativa Pos sarà l'asse Y e l'asse X.sarà anthor X variabile (quantitativa). La lunghezza della barra orizzontale per ciascun valore Pos è definita dal punto iniziale e finale. Ad esempio, la riga per 1 inizierà da 1 e terminerà a 3 nell'asse x.
Quello che segue è un abbozzo approssimativo dell'output desiderato.
risposte:
3 per risposta № 1Usa il pacchetto ggplot2
con geom_segment
disegnare le linee.
Inizia combinando i tuoi dati in a data.frame
, poiché questa è la struttura dati richiesta per 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)
)
Crea la trama:
library(ggplot2)
ggplot(dat) +
geom_segment(aes(x=start, y=pos, xend=end, yend=pos), color="blue", size=3) +
scale_y_reverse()
5 per risposta № 2
Nella base R ...
plot(pos, type = "n", xlim = range(c(start, end)), ylim = c(13,0))
grid()
segments(start, pos, end, pos)
Per farlo più esattamente come la tua figura ...
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)