2

I'm trying to produce a graph with points within shaded areas: "low_risk", "mid_risk" or "high_risk".

I tried using geom_curves for the borders but don't know how to shade between them in yellow, orange and red.

library("dplyr")
library("ggplot2")

Likelihood <- c(2,1,2,1,1,2,2,3,2,1,3,2,1,1,2,4,4,2,2,2,2,3,2,2,1,1,2,2,2)
Impact <- c(4,3,2,2,2,3,1,2,2,3,2,3,3,4,3,2,2,2,2,4,3,1,1,4,4,4,3,2,4)
RISK <- c("Reg","Con","Strat","Con","Rep","Reg","Reg","Op","Op","L","Op","Strat","Strat","Op","Con","Reg","Reg","Op","Strat","Op","Reg","Rep","Op","Con","Op","Reg","Strat","Op","Con")
Weight <- Likelihood*Impact/64

df = data.frame(RISK,Likelihood,Impact,Weight)

low_risk <- geom_curve(aes(x = 1, xend = 4, y = 4, yend = 1), color = "yellow", size = 1)
mid_risk <- geom_curve(aes(x = 2, xend = 4, y = 4, yend = 2), color = "orange", size = 1)
high_risk <- geom_curve(aes(x = 3, xend = 4, y = 4, yend = 3), color = "red", size = 1)

Graph <- ggplot(data = df, aes(x = Likelihood, y = Impact, color = RISK, size = Weight, fill = RISK)) +
  geom_jitter(width = 0.2, height = 0.2, shape = 21, stroke = 2) + 
  scale_size_continuous(range = c(4, 16)) +
  scale_x_continuous(limits = c(.8, 4.2)) + scale_y_continuous(limits = c(.8, 4.2)) +
  low_risk + mid_risk + high_risk

NOTE: Managed to do it in a more convoluted way and still wondered if there is a more elegant solution...

library("dplyr")
library("ggplot2")

high_zone <- function(x) {
  12/x
}

mid_zone <- function(x) {
  8/x}

low_zone <- function(x) {
  4/x
}

Likelihood <- c(2,1,2,1,1,2,2,3,2,1,3,2,1,1,2,4,4,2,2,2,2,3,2,2,1,1,2,2,2)
Impact <- c(4,3,2,2,2,3,1,2,2,3,2,3,3,4,3,2,2,2,2,4,3,1,1,4,4,4,3,2,4)
RISK <- c("Reg","Con","Strat","Con","Rep","Reg","Reg","Op","Op","L","Op","Strat","Strat","Op","Con","Reg","Reg","Op","Strat","Op","Reg","Rep","Op","Con","Op","Reg","Strat","Op","Con")
Weight <- Likelihood*Impact/64

Graph <- ggplot(data = Weight) +
  geom_jitter(aes(x = Likelihood, y = Impact, size = Weight, fill = RISK), stroke = 1.2, color = "black", shape = 21, width = 0.20, height = 0.20) + 
  scale_size_continuous(range = c(4, 16)) +
  stat_function(fun = high_zone, geom = "line", color = "red", size = 0.8, alpha = 0.5) +
  stat_function(fun = mid_zone, geom = "line", color = "orange", size = 0.8) + 
  stat_function(fun = low_zone, geom = "line", color = "yellow", size = 0.8) +
  scale_x_continuous(limits = c(0.8,4.2)) + scale_y_continuous(limits = c(0.8,4.2))

gg1 <- ggplot_build(Graph)

df1 <- data.frame(low_like = gg1$data[[4]]$x,
                  low_like_min = gg1$data[[4]]$y,
                  mid_like_max = gg1$data[[3]]$y)

df2 <- data.frame(mid_like1 = gg1$data[[3]]$x[40:101],
                  mid_like_min1 = gg1$data[[3]]$y[40:101],
                  high_like_max1 = gg1$data[[2]]$y[40:101])

top_boundary <- function(x) {
  4.18
}

Graph1 <- Graph + stat_function(fun = top_boundary, geom = "line", color = "red", size = 0.0001, alpha = 0.0001)

Graph2 <- Graph1 + geom_ribbon(data = df1, aes(x = low_like, ymin = low_like_min, ymax = mid_like_max),
                               fill = "yellow", alpha = 0.3)

gg2 <- ggplot_build(Graph2)

df12 <- data.frame(lows = gg2$data[[5]]$x[1:34],
                   low_line = gg2$data[[4]]$y[1:34],
                   bounds = gg2$data[[5]]$y[1:34])

df22 <- data.frame(lows1 = gg2$data[[5]]$x[34:62],
                   low_line1 = gg2$data[[3]]$y[34:62],
                   bounds1 = gg2$data[[5]]$y[34:62])

df32 <- data.frame(lows2 = gg2$data[[5]]$x[62:101],
                   low_line2 = gg2$data[[2]]$y[62:101],
                   bounds2 = gg2$data[[5]]$y[62:101])

Graph3 <- Graph2 + geom_ribbon(data = df12, aes(x = lows, ymin = low_line, ymax = bounds),
                               fill = "yellow", alpha = 0.3) +
  geom_ribbon(data = df2, aes(x = mid_like1, ymin = mid_like_min1, ymax = high_like_max1),
              fill = "orange", alpha = 0.3) +
  geom_ribbon(data = df22, aes(x = lows1, ymin = low_line1, ymax = bounds1), 
              fill = "orange", alpha = 0.3) +
  geom_ribbon(data = df32, aes(x = lows2, ymin = low_line2, ymax = bounds2), 
              fill = "red", alpha = 0.3)
  • 2
    Please provide example data, and remove irrelevant code, for example *theme_* is irrelevant there so is *readxl*. – zx8754 Nov 28 '19 at 13:47
  • please read https://stackoverflow.com/help/how-to-ask and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and https://stackoverflow.com/help/minimal-reproducible-example. Then modify your question accordingly. Going through those steps very very often results in getting to the core of the problem by yourself. If it still doesnt help, we will be much more willing to help you when there is a concise example and problem – tjebo Nov 28 '19 at 15:59
  • Hello, Thanks for the notes - made those edits now – Kevin Wyatt Nov 29 '19 at 09:34

0 Answers0