I'd like to gradient fill a ggplot2
bar plot
based on the values of Y, but I'd like to manually set the colours. This example is almost what I need, but it sets the limits of the colour palette based on the values of Y, whereas I want to set these manually based on a biological stress threshold
Here's some sample code based on the linked example.
d <- data.frame(x = 1:4, y= c (0.1, 0.3, 1, 0.6))
# interpolate values from zero to y and create corresponding number of x values
vals <- lapply(d$y, function(y) seq(0, y, by = 0.01))
y <- unlist(vals)
mid <- rep(d$x, lengths(vals))
d2 <- data.frame(x = mid - 0.4,
xend = mid + 0.4,
y = y,
yend = y)
ggplot(data = d2, aes(x = x, xend = xend, y = y, yend = yend, color = y)) +
geom_segment(size = 2) +
scale_color_gradient2(low = "green4", mid = "yellow", high = "red",
midpoint = max(d2$y)/2) +
coord_cartesian(ylim = c(0 , 2))
I'd like the red to appear when the Y value is equal to 2, rather than the maximum value of Y in the data frame.