Search code examples
rplotgraphicsfigure

R (base): Adding points with confidence levels to existing plot


I have a plot with R base:

plot(c(1,2), c(2,3), type="l")
points(c(1.2), c(2.2))

I now want to add a new data set to this plot that includes confidence levels. Here is how I would plot it:

library("plotrix")
set.seed(0815)
x <- 1:2
F <- runif(2,1,2) 
L <- runif(2,0,1)
U <- runif(2,2,3)
plotCI(x, F, ui=U, li=L)

But this creates a new plot, it doesn't add it to the first plot I made. Is it possible to combine them in one, single graph?


Solution

  • plot and plotCI both automatically create whole new plots. You could use lines instead of plot and change the order:

    library("plotrix")
    set.seed(0815)
    x <- 1:2
    F <- runif(2,1,2) 
    L <- runif(2,0,1)
    U <- runif(2,2,3)
    
    plotCI(x, F, ui=U, li=L)
    lines(c(1,2), c(2,3), type="l")
    points(c(1.2), c(2.2))