I try to create a Scatterplot where i.e. the background of the area from 0 to 25 on the x axis is red, the area from 25 to 75 is yellow and the area from 75 to 100 ist green, so you can see immediately which values are critical.
I looked up all the questions regarding this topic, but none seems to fit my question. Here is a short reproducable example:
library(tidyverse)
library(ggpubr)
v1 <- c(1, 1, 1)
v2 <- c(1, 0, 0)
v3 <- c(1, 0, 1)
v4 <- c(0, 1, 1)
xG <- c(3, 3, 3, 3)
input <- c(v1, v2, v3, v4)
df <- data_frame(values = input,
module = c(rep("A", length(v1)),
rep("B", length(v2)),
rep("C", length(v3)),
rep("D", length(v4))))
perWorkField <- df %>%
group_by(module) %>%
summarise(sums = sum(values)) %>%
mutate(percent = round((sums / xG) * 100, 2))
g <- ggscatter(data = perWorkField,
x = "percent",
y = "module",
shape = 4)
g <- ggpar(g, xlim = c(0, 100))
print(g)
I prefer to use ggpubr for plotting but it's fully compatible to ggplot2. I'm greatful for any help. Thanks in advance!
EDIT: Obviously ggpubr isn't the best solution for this task as the points are in the background behind the colored areas. With ggplot2 the solution would be:
ggplot(data = perWorkField) +
geom_point(mapping = aes(x = percent, y = module), shape = 20) + xlim(c(0, 100)) +
geom_polygon(aes(x=c(0,25,25,0),
y=c(0.5,0.5,4.5,4.5)),fill="#F5817A",color=NA) +
geom_polygon(aes(x=c(25,75,75,25),
y=c(0.5,0.5,4.5,4.5)),fill="#FFFF0044",color=NA) +
geom_polygon(aes(x=c(75,100,100,75),
y=c(0.5,0.5,4.5,4.5)),fill="#00FF0044",color=NA) +
geom_point(mapping = aes(x = percent, y = module)) + theme_light()
geom_polygon
is a simple solution for coloring areas of the plot.