lattice xyplot panel.abline - how to add different horizontal lines based on x values?
lattice, r
Solution
Edit: There is only one petal.width value that equals 0.5 and this identifies it and draws a horizontal line at the associated sepal.length value.
xyplot(sepal.length ~ petal.width | iris.type, data = iris,
panel = function( x,y,...) {
panel.abline( h=y[ which(x==0.5) ], lty = "dotted", col = "black")
panel.xyplot( x,y,...)
})
I tested to make sure that it also handles multiple matches in multiple panels, which it does. If you wanted to test for multiple values it would be:
... (h=y[ which(x %in% values) ] , ...
And if I don't, then somebody will surely come along and point out that the `which` is not needed, since R supports logical indexing as well as numeric indexing.
Problem
How can I draw horizontal lines in each graph based on specified x values? For example, when I have X=1 on x-axis, the matched dot on the plot is (1,y0), and then consequently draw a horizontal line Y=y0? A working example using iris data with only a vertical line at x=0.5: ``` iris = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", sep=',', header=F) names(iris) = c("sepal.length", "sepal.width", "petal.length", "petal.width", "iris.type") xyplot(sepal.length ~ petal.width | iris.type, data = iris, panel = function(...) { panel.abline(v=0.5, lty = "dotted", col = "black") panel.xyplot(...) }) ``` But I'd also want to have horizontal lines shown in this way: See in the `iris-setosa` graph(panel), a horizontal line is marked through the point at (0.5,y)--I draw this manually. I don't know how to specify y in the panel.abline, since y is a variable that appears different for each panel. In my actual data, my x and y have one-to-one relation. I thought it should be a simple problem, but have no idea how to work this around. I hope this is clearer.