Reputation: 3
I am working on building a plot to map intervals of time for several items of interest.
To do this, I have built a dumbbell plot with ggalt. I am trying to unify my colour schemes across multiple graphs to ensure the same time periods are the same colours across graphs. In particular, I am creating other graphics where "Interval 1" is important and highlighted elsewhere; having uniform colour schemes will help interpretation.
Can geom_dumbbell recognize my custom palette as I would for other plots usually? I would like to have the custom palette to be modifiable globally rather than being defined at each graph.
The only similar question that I can find is this one which unfortunately just overlays a geom_point to produce a legend... This might work but seems quite clunky.
library(ggplot2)
library(ggalt)
df <- tibble(x = c(4,6,5,8,10), y = c(8,9,10,12,15), z = c(14,12,14,15,18), d = c("A","B","C","D","E"))
legend_colors<- c("Interval 1" = viridis(1, option = "C", begin = 0, end = 0.1),
"Interval 2" = viridis(1, option = "C", begin = 0.1, end = 0.2),
"Interval 3" = viridis(1, option = "C", begin = 0.5, end = 0.7))
df %>%
ggplot() +
geom_dumbbell(aes(x = x,
xend = y,
y = d),
colour = "Interval 1",
colour_xend = "Interval 2",
size = 2) +
geom_dumbbell(aes(x = y,
xend = z,
y = d),
colour = "Interval 2",
colour_xend = "Interval 3",
size = 2) +
scale_colour_manual(values = legend_colors) +
theme_clean()
I want to produce a graphic like this but with colours that are defineable outside of the graph code itself. Sample Graphic
Upvotes: 0
Views: 32
Reputation: 125373
One option to achieve your desired result would be to reshape your data to long and use a geom_point
+ geom_line
:
library(ggplot2)
library(viridis)
library(ggthemes)
library(tidyr)
names(legend_colors) <- c("x", "y", "z")
df |>
pivot_longer(-d) |>
ggplot(aes(value, d)) +
geom_line(aes(group = d, color = name), size = 1) +
geom_point(aes(color = name), size = 2) +
scale_color_manual(
values = legend_colors
) +
theme_clean()
Upvotes: 0