ggplot图例未显示 [英] ggplot legend not displyed

查看:114
本文介绍了ggplot图例未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码未显示图例:

library(ggplot2)
g=ggplot() 
g=g+geom_line(aes(x=1:10,y=1:10),color="red",size=0.2)
g=g+geom_line(aes(x=5:12,y=15:22),color="green",size=0.2)
g=g+theme(legend.position = c(0, 1),legend.justification = c(0, 1))
g=g+scale_color_manual(values = c("red","green"))
g

我在互联网上搜索了所有答案,但都没有成功.请注意,我不能使用ggplot(aes(...))或使用数据框,因为这两行的x坐标不同.

I have searched all over the internet for the answer without any success. Note that I cannot use ggplot(aes(...)) or use a dataframe because the two lines have different x-coordinates.

推荐答案

您需要在 aes 调用中映射颜色-现在,没有要显示的色标.

You need to map the colors in the aes call - right now, there's no color scale to show.

例如,考虑这一点

library(ggplot2)
colors <- c("L1" = "red", "L2" = "green")
g=ggplot() 
g=g+geom_line(aes(x=1:10,y=1:10, color="L1"),,size=0.2)
g=g+geom_line(aes(x=5:12,y=15:22, color="L2"),size=0.2)
g=g+theme(legend.position = c(0, 1),legend.justification = c(0, 1))
g=g+scale_color_manual(values = colors)
g

这篇关于ggplot图例未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆