How to add new dots to existing lattice plot in R

后端 未结 1 458
闹比i
闹比i 2021-01-17 02:02

I used the lattice package to draw a line plot.

library(lattice)  
xyplot(price~month,groups=perc,data=Edf,type=\'l\',
       main=\"Percent         


        
相关标签:
1条回答
  • 2021-01-17 02:57

    For the sake of completeness, here is a reproducible example. Simply store the created xyplot in a variable and then use update along with a custom panel function to add additional points.

    library(lattice)
    
    ## create scatterplot
    p <- xyplot(1:10 ~ 1:10)
    
    ## insert additional points
    update(p, panel = function(...) {
      panel.xyplot(...)
      panel.xyplot(1:10, 10:1, pch = 4, col = "orange")
    })
    

    Alternatively, you can also create a second xyplot and use as.layer from latticeExtra to add it to your initial plot.

    library(latticeExtra)
    
    ## create second scatterplot and add it to first plot
    p2 <- xyplot(10:1 ~ 1:10, pch = 4, col = "orange")
    p + as.layer(p2)
    

    Or, as suggested by @Pascal, use layer alongside with panel.points to achieve your goal.

    p + layer(panel.points(1:10, 10:1, pch = 4, col = "orange"))
    
    0 讨论(0)
提交回复
热议问题