Is there a possible way to shade an area between two lines
using Base R given these two constraints:
- I can't use
ggplot2
because other parts of my app will not work (aShiny
clickId
limitation; more specifically, usingggplot2
'sclickId
does not give the real x and y values of the data). - I also can't use
polygon
becauseNA
s are present in my data, which makes the "shade" created bypolygon
close prematurely.
Here is a SSCCE (excluding the Shiny
components):
#my sample data
vals_time <- c(1:10)
vals_line1 <- c(8, 9, NA, NA, 7, 8, 7, NA, 9, 4)
vals_line2 <- c(4, 5, NA, NA, 6, 10, 8, NA, 5, 2)
#create line graphs without the shade
plot(x = vals_time, y = vals_line1, type = "l", ylim = c(0,11))
lines(x= vals_time, y = vals_line2)
Here's how I used the polygon
function:
polygon(x = c(vals_time, vals_time[10:1]),
y = c(vals_line1, vals_line2[10:1]),
col = rgb(1, 0, 0, 0.5))