我使用xyplot()
中的Lattice
来绘制由3列数据框定义的箭头:posi(数字),从(字符)到(字符)。问题:有时,箭头超出了绘图的范围。换句话说,绘图窗口不够大,无法显示所有数据。
我尝试明确添加因子水平,但无济于事。似乎如果更多的极端水平(例如" D")不存在于因子" df $来自",那些不计算绘制绘图窗口。我查看ylim
但这仅限于数值。
我已经环顾四周了,并且发现了很多关于重新缩放,重新排序轴但没有任何帮助我解决手头问题的事情。这个问题与@skan的一个问题有关:How to plot segments or arrows in Lattice。
我的数据:
l <- c("A", "B", "C", "D")
df <- data.frame(posi = c(1, 2, 3, 4, 5),
from = factor(c("A", "B", "C", "D", "A"), levels = l, ordered = TRUE),
to = factor(c("C", "D", "D", "C", "A"), levels = l, ordered = TRUE)
)
这是预期的情节:
xyplot(from ~ posi , type="p", col="black", data=df, pch=16, xlim = c(0,7),
panel = function(...){
panel.dotplot(x = df$posi, y = df$from, col ="green", cex=1.6)
panel.dotplot(x = (df$posi+1), y = df$to, col="black", cex=1.)
panel.arrows(x0 = df$posi, y0 = df$from, x1 = df$posi+1, y1 = df$to, lwd=2, col="blue" )
}
)
当我只使用前三行时,相同的数据框与'#39; old&#39;因子水平完整,该情节没有考虑到后来的&#34; D&#34;需要水平。
## only first 3 rows, without element "D" in the factor df$from
df <- df[1:3, ]
导致这个情节:
我希望能够设置Y轴的限制,并希望得到任何帮助或提示。
答案 0 :(得分:1)
通过在调用xyplot中指定“drop.unused.levels = FALSE”可以避免这个问题。
library(lattice)
l <- c("A", "B", "C", "D")
df <- data.frame(posi = c(1, 2, 3, 4, 5),
from = factor(c("A", "B", "C", "D", "A"), levels = l,
ordered = TRUE),
to = factor(c("C", "D", "D", "C", "A"), levels = l,
ordered = TRUE))
xyplot(from ~ posi , type="p", col="black", data=df, pch=16, xlim = c(0,7),
panel = function(...){
panel.dotplot(x = df$posi, y = df$from, col ="green", cex=1.6)
panel.dotplot(x = (df$posi+1), y = df$to, col="black", cex=1.)
panel.arrows(x0 = df$posi, y0 = df$from, x1 = df$posi+1, y1 = df$to,
lwd=2, col="blue" )
})
df2 <- df[1:3,]
xyplot(from ~ posi , type="p", col="black", data=df2, pch=16, xlim = c(0,7),
drop.unused.levels = FALSE,
panel = function(...){
panel.dotplot(x = df2$posi, y = df2$from, col ="green", cex=1.6)
panel.dotplot(x = (df2$posi+1), y = df2$to, col="black", cex=1.)
panel.arrows(x0 = df2$posi, y0 = df2$from, x1 = df2$posi+1,
y1 = df2$to, lwd=2, col="blue" )
})