Adding abline of different data to xyplot panels

后端 未结 2 1580
一整个雨季
一整个雨季 2021-01-21 06:18

Using lattice in R, I have plotted an xyplot using the factors option. Now I want to add a horizontal abline to every plot, however out of another dataframe. How can I get these

2条回答
  •  广开言路
    2021-01-21 06:53

    I think you have to use a custom panel function.

    library(lattice)
    
    MyData <- data.frame(
      v1 = 1:50,
      v2 = rnorm(50,20,5),
      v3 = c(rep("one", 10), rep("two", 10),  rep("three", 10), rep("four", 10), rep("five", 10)))
    
    lineData <- c(30,30,20,20,40) 
    # define custom panel plot function
    customPanel = function(x, y, lineData) {
      panel.xyplot(x, y)
      panel.abline(h = lineData[panel.number()]) # instead of using a global variable
    }
    
    xyplot(
      v1 ~ v2 | v3,
      data = MyData,
      layout = c(5, 1),
      scales = list(
        x = list(relation="free"),
        y = list(relation="same")
      ),
      panel = customPanel,
      lineData = lineData)
    

提交回复
热议问题