Here is a sample ggplot dot chart I created:

For this I used the following code:
mtcars$cyl<-as.character(mtcars$cyl)
ggplot(
mtcars,
aes(fill=cyl,
y=gear,
x=mpg)) +
geom_line(aes(group = gear)) +
geom_vline(xintercept = mean(mtcars$mpg), linetype="dotted",
color = "black", size=1) +
geom_point(aes(color = cyl),size=5) +
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
What I actually want is a second legend describing that the dotted line is the overall mean. It could look for example like that:

How can I do that?
This can be one solution:
library(tidyverse)
mtcars$cyl<-as.character(mtcars$cyl)
d_me <-
mtcars %>%
summarize(me = mean(mpg)) %>%
mutate(var = "overall \n mean")
mtcars %>%
ggplot(
aes(fill=cyl,
y=gear,
x=mpg)) +
geom_line(aes(group = gear)) +
geom_point(aes(color = cyl),size=5) +
geom_vline(data = d_me, aes(xintercept = me, linetype = var),
color = "black", size=1) +
scale_linetype_manual(values = "dotted", name = "") +
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With