0

I used the lattice package to draw a line plot.

library(lattice)  
xyplot(price~month,groups=perc,data=Edf,type='l',
       main="Percentile chart of OpRes Charge Rates Forcast", 
       ylab="OpRes Charge Rate ($/MWh)", xlab="Months",ylim=c(0,40),auto.key=TRUE)

Then I wanted to add some dots to the existing plot.

points(rep(1,length(OpResWestJan)),OpResWestJan) 

OpResWestJan is a vector, but the dots never appeared in the existing plot, and there were no warnings.

fdetsch
  • 5,239
  • 3
  • 30
  • 58
hassISC
  • 121
  • 2
  • 3
  • 7
  • 4
    You cannot use base graphics function ( like `points()`) with Lattice graphics commands (like `xyplot`). You would need to create a custom panel function to work with lattice. It's hard to help you any further since you did not provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jun 30 '15 at 01:45
  • Maybe this question can help you: http://stackoverflow.com/questions/15803149/how-to-add-points-to-multi-panel-lattice-graphics-bwplot – MrFlick Jun 30 '15 at 01:46
  • 1
    You can use `latticeExtra` package. You can then combine `xyplot()`, `layer()` and `panel.points()` with `+`. –  Jun 30 '15 at 03:05

1 Answers1

6

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

scatterplot

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"))
fdetsch
  • 5,239
  • 3
  • 30
  • 58