There's no way to do this directly using theme
, since element_text
won't take vectorized input. Probably easiest to fake it by turning clipping off and plotting some text where the legend should be:
library(ggplot2)
ggplot(mtcars, aes(wt, mpg, colour = factor(cyl))) +
geom_point() +
geom_text(data = data.frame(wt = c(6, 6, 6, 6), mpg = c(20, 22.5, 25, 27.5),
cyl = c(levels(factor(mtcars$cyl)), "cyl"),
colour = c(levels(factor(mtcars$cyl)), "cyl")),
aes(label = colour)) +
coord_cartesian(xlim = c(1.5, 5.5), clip = "off") +
scale_color_manual(values = c("blue", "green", "red", "black")) +
theme(legend.position = "none",
plot.margin = margin(10, 50, 10, 10))
