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" )
}
)
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:
I want to be able to set the limits of the Y-axis, and would appreciate any help or hint.