晶格xyplot并未在Y轴上显示所有水平的因子:不完整的图 [英] Lattice xyplot does not show all levels of factor on Y axis: incomplete plot

查看:101
本文介绍了晶格xyplot并未在Y轴上显示所有水平的因子:不完整的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Lattice中的xyplot()来绘制由具有3列的数据框定义的箭头:位置(数字),从(字符)到(字符).问题:箭头有时会超出情节的范围.换句话说,绘图窗口不足以可视化所有数据.

I use xyplot() from the Lattice to plot arrows defined by a dataframe with 3 columns: posi (numerical), from (character), to (character). The problem: On occasion, the arrows go beyond the scale of the plot. In other words, the plot window is not big enough to visualize all the data.

我尝试显式添加因子水平,无济于事.似乎如果因子"df $ from"中不存在更极端的水平(例如"D"),则不计入绘制绘图窗口的水平.我看着ylim,但这仅限于数值.

I tried adding the factor levels explicitly, to no avail. It seems that if the more extreme levels (e.g. "D") are not present in the factor "df$from", those are not counted to draw the plot window. I looked at ylim but this is limited to numerical values.

我环顾四周,发现了很多有关重新缩放比例,重新排序轴的信息,但是没有什么可以帮助我解决当前的问题.此问题与@skan中的一个问题有关:

I have looked around on SO, and found a lot concerning rescaling, reordering axes but nothing that helped me with the problem at hand. This question is related to a question from @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" )
       }
)

当我只使用前三行时,相同数据帧的旧"因子水平保持不变,则该图未考虑到以后将需要"D"水平.

When I only use the first 3 rows, of the same data frame with 'old' factor levels intact, the plot does not take into account that later on a "D" level will be needed.

## only first 3 rows, without element "D" in the factor df$from
df <- df[1:3, ] 

此图的结果:

我希望能够设置Y轴的限制,希望对您有所帮助或提示.

I want to be able to set the limits of the Y-axis, and would appreciate any help or hint.

推荐答案

可以通过在xyplot调用中指定"drop.unused.levels = FALSE"来避免此问题.

The problem can be avoided by specifying "drop.unused.levels = FALSE" in the call to xyplot.

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

这篇关于晶格xyplot并未在Y轴上显示所有水平的因子:不完整的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆