This example was drawn in Inkscape-but I need a vertical gradient-not horizontal.
Interval from zero to positive == from white to red.
Interval from zero< /strong> to negative == from white to red.
Is it possible to do this A little bag?
I made up some source data….
set.seed(1)
x<-seq(from = -10, to = 10, by = 0.25)
data <- data.frame(value = sample(x, 25, replace = TRUE), time = 1:25)
plot(data$time, data$value , type = "n")
my.spline <- smooth.spline(data$time, data$value, df = 15)
lines(my.spline$x, my.spline$y, lwd = 2.5, col = "blue")
abline(h = 0)
shade <- function(x, y, col, n=500, xlab='x', ylab='y', ...) {
# x, y: the x and y coordinates
# col: a vector of colours (hex, numeric, character), or a colorRampPalette
# n: the vertical resolution of the gradient
# ...: further args to plot()
plot (x, y, type='n', las=1, xlab=xlab, ylab=ylab, ...)
e <- par('usr')
height <- diff(e [3:4])/(n-1)
y_up <- seq(0, e[4], height)
y_down <- seq(0, e[3], -height)< br /> ncolor <- max(length (y_up), length(y_down))
pal <- if(!is.function(col)) colorRampPalette(col)(ncolor) else col(ncolor)
# plot rectangles to simulate colour gradient< br /> sapply(seq_len(n),
function(i) {
rect(min(x), y_up[i], max(x), y_up[i] + height, col=pal [i], border=NA)
rect(min(x), y_down[i], max(x), y_down[i]-height, col=pal[i], border=NA)
})
# plot white polygons representing the inverse of the area of interest
polygon(c(min(x), x, max(x), rev(x)),
c (e[4], ifelse(y> 0, y, 0),
rep(e[4], length(y) + 1)), col='white', border=NA)
polygon(c(min(x), x, max(x), rev(x)),
c(e[3], ifelse(y <0, y, 0),
rep (e[3], length(y) + 1)), col='white', border=NA)
lines(x, y)
abline(h=0)
box ()
}
Here are some examples:
xy <- curve(sin, -10, 10, n = 1000)
shade(xy$x, xy$y, c('white','blue'), 1000)
Or use the color gradient palette to specify the color:
shade(xy$x, xy$y, heat.colors, 1000)
And applied to your data, although we first interpolate the points to a finer resolution (if we don’t do this, the gradient will not follow the line where it crosses zero).
p>
xy <- approx(my.spline$x, my.spline$y, n=1000)
shade(xy$x, xy$y, c('white','red') , 1000)
How to fill the area below the (sp) line with a gradient color?
This example was drawn in Inkscape-but I need a vertical gradient-not horizontal.
Interval from zero to positive == from white to red.
Interval from zero< /strong> to negative == from white to red.
Is it possible to do this A little bag?
I made up some source data….
set.seed(1)
x<-seq(from = -10, to = 10, by = 0.25)
data <- data.frame(value = sample(x, 25, replace = TRUE), time = 1:25)
plot(data$time, data$value , type = "n")
my.spline <- smooth.spline(data$time, data$value, df = 15)
lines(my.spline$x, my.spline$y, lwd = 2.5, col = "blue")
abline(h = 0)
This is a method in basic R, we use The gradient color rectangle fills the entire drawing area, and then fills the inversion of the area of interest with white.
shade <- function(x, y, col, n= 500, xlab='x', ylab='y', ...) {
# x, y: the x and y coordinates
# col: a vector of colours (hex, numeric, character ), or a colorRampPalette
# n: the vertical resolution of the gradient
# ...: further args to plot()
plot(x, y, type='n', las =1, xlab=xlab, ylab=ylab, ...)
e <- par('usr')
height <- diff(e[3:4])/(n-1)
y_up <- seq(0, e[4], height)
y_down <- seq(0, e[3], -height)
ncolor <- max(length(y_up) , length(y_down))
pal <- if(!is.function(co l)) colorRampPalette(col)(ncolor) else col(ncolor)
# plot rectangles to simulate colour gradient
sapply(seq_len(n),
function(i) {
rect(min(x), y_up[i], max(x), y_up[i] + height, col=pal[i], border=NA)
rect(min(x), y_down[i] , max(x), y_down[i]-height, col=pal[i], border=NA)
})
# plot white polygons representing the inverse of the area of interest
polygon(c(min(x), x, max(x), rev(x)),
c(e[4], ifelse(y> 0, y, 0),
rep( e[4], length(y) + 1)), col='white', border=NA)
polygon(c(min(x), x, max(x), rev(x)),
c(e[3], ifelse(y <0, y, 0),
rep(e[3], length(y) + 1)), col='white', border= NA)
lines(x, y)
abline(h=0)
box()
}
Here are some examples:
< p>
xy <- curve(sin, -10, 10, n = 1000)
shade(xy$x, xy$y, c('white','blue'), 1000)
Or use the color gradient palette to specify the color:
shade(xy$x, xy$y, he at.colors, 1000)
and applied to your data, although we first interpolate the points to a finer resolution (if we don’t do this, the gradient will not follow it through zero Line).
xy <- approx(my.spline$x, my.spline$y, n=1000)
shade(xy$x, xy$ y, c('white','red'), 1000)