0

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.

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.

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.

My data:

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

This plots as expected:

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

enter image description here

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, ] 

resulting in this plot:

enter image description here

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

KoenV
  • 4,113
  • 2
  • 23
  • 38

1 Answers1

1

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

complete data

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

subset of data with unused factor levels

Konn
  • 108
  • 6