Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 未显示ggplot图例_R_Ggplot2 - Fatal编程技术网

R 未显示ggplot图例

R 未显示ggplot图例,r,ggplot2,R,Ggplot2,以下代码不显示图例: 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

以下代码不显示图例:

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坐标。

为此,您应该在
aes
中添加
color

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 

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

例如,考虑这个

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
库(ggplot2)

传说中伤害我眼睛的颜色<代码>缩放\u颜色\u手册()
急需