以下是随机图的示例代码:
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())
以下是上述代码的输出:
我的要求:我只希望将图例文本“ 0”和“ 1”分别着色为“红色”和“天蓝色”(与线的颜色相同),而不要使用由0和1表示的两条线作为图例元素,并删除这些线。这可能吗?
我在ggh4x软件包中编写了一个字符串图例指南,您可能会发现它很有用。
例子:
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())
由reprex软件包(v0.3.0)创建于2021-03-19
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句