Using geom_rect
I am separating my data into classes in a ggplot
.
These classes (v high, high, medium, low, v low) are denoted in the legend.
Is there a possibility to add the lower and upper limits of the classes from the data.frame
to the legend symbols?
Here is my set up:
library(ggplot2)
library(viridis)
dat <- data.frame(x = seq(1,100,by=2), y = seq(1,300,by=3))
df <- data.frame(class = c("very high", "high", "medium", "low", "very low"),
lwr = c(90, 75, 35, 15, 0),
upr = c(100, 90, 75, 35, 15))
ggplot(dat) +
geom_rect(data = df, aes(xmin = lwr, xmax = upr, ymin = -Inf, ymax = Inf, fill = class), alpha = 0.4)+
geom_point(aes(x, y)) +
theme_bw()+
scale_fill_viridis("Classes", discrete = TRUE, option = "viridis",
limits = c("very high", "high", "medium", "low", "very low"))
I would like the legend to follow the following principle: Legend depicting the classes as well as their ranges.
So far, I have only found ways to change the label itself, but no indication on how to add information from the data frame to the legend or how to widen the symbols enough for text to fit in. Do you have any suggestions?