Following is the sample code for the random chart:
library(ggplot2)
ggplot(mtcars,aes(x=mpg,y=wt,color=factor(vs)))+geom_line()+theme(legend.position="top",legend.direction = "horizontal",legend.title=element_blank(),legend.key = element_blank())
and following is the output from above code:
My requirement: Instead of having two lines denoted by 0 and 1 as legend elements, I only want legend texts "0" and "1" colored as "red" and "skyblue" (same as color of lines) and remove the lines. Is this possible?
I've written a string legend guide in the ggh4x package, that you might find useful.
Example:
library(ggplot2)
library(ggh4x)
ggplot(mtcars,aes(x=mpg,y=wt,color=factor(vs))) +
guides(colour = "stringlegend") +
geom_line() +
theme(legend.position="top",
legend.direction = "horizontal",
legend.title=element_blank(),
legend.key = element_blank())
Created on 2021-03-19 by the reprex package (v0.3.0)
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