R ggplot 更改图例中的颜色和图例序列
R ggplot changing the color and legend sequences in legend
此 post 中的数据: 保持不变 我又添加了一列。通过 Jimbou 的给定解决方案的帮助,我编写了以下代码
head(AA)
States Clusters value Comp_apply L1_25 L1_30 L1_50 x xbreaks
<chr> <chr> <dbl> <int> <int> <int> <int> <dbl> <dbl>
1 HR Cluster-1 0.0703 9 2 4 7 31 32.7
2 HR Cluster-2 0.0761 4 2 2 3 33 32.7
3 HR Cluster-3 0.0692 9 7 7 8 34 32.7
4 WB Cluster-1 0.0372 13 2 2 2 111 113.
5 WB Cluster-2 0.0762 13 2 3 6 113 113.
6 WB Cluster-3 0.0906 13 3 3 4 114 113.
现在绘图的代码是
ggplot(data=data.m1)+
geom_line(aes(x = x, y = Comp_apply, group=States,color="red")
,position=position_dodge(width = 0.90))+
geom_point(aes(x = x, y = Comp_apply, group=States,color="red")
,position=position_dodge(width = 0.90))+
geom_line(aes(x = x, y = L1_25, group=States,color="green")
,position=position_dodge(width = 0.90))+
geom_point(aes(x = x, y = L1_25, group=States,color="green")
,position=position_dodge(width = 0.90))
现在剧情如下
你能帮我纠正一下吗
如果你想为整个 geom
分配颜色,只需将 colour="red"
放在 aes()
之外。但随后颜色将不会出现在图例中,因为它没有映射到任何因素。
正确的做法是修改 data.frame
,使 Comp_apply
和 L1_25
显示为同一因素的两种模式(即在同一列中)。我没法帮你做,因为你的数据没有提供。
那么您只需调用 geom_point
和 geom_line
一次。提供数据,我更新我的答案。
此 post 中的数据:
head(AA)
States Clusters value Comp_apply L1_25 L1_30 L1_50 x xbreaks
<chr> <chr> <dbl> <int> <int> <int> <int> <dbl> <dbl>
1 HR Cluster-1 0.0703 9 2 4 7 31 32.7
2 HR Cluster-2 0.0761 4 2 2 3 33 32.7
3 HR Cluster-3 0.0692 9 7 7 8 34 32.7
4 WB Cluster-1 0.0372 13 2 2 2 111 113.
5 WB Cluster-2 0.0762 13 2 3 6 113 113.
6 WB Cluster-3 0.0906 13 3 3 4 114 113.
现在绘图的代码是
ggplot(data=data.m1)+
geom_line(aes(x = x, y = Comp_apply, group=States,color="red")
,position=position_dodge(width = 0.90))+
geom_point(aes(x = x, y = Comp_apply, group=States,color="red")
,position=position_dodge(width = 0.90))+
geom_line(aes(x = x, y = L1_25, group=States,color="green")
,position=position_dodge(width = 0.90))+
geom_point(aes(x = x, y = L1_25, group=States,color="green")
,position=position_dodge(width = 0.90))
现在剧情如下
你能帮我纠正一下吗
如果你想为整个 geom
分配颜色,只需将 colour="red"
放在 aes()
之外。但随后颜色将不会出现在图例中,因为它没有映射到任何因素。
正确的做法是修改 data.frame
,使 Comp_apply
和 L1_25
显示为同一因素的两种模式(即在同一列中)。我没法帮你做,因为你的数据没有提供。
那么您只需调用 geom_point
和 geom_line
一次。提供数据,我更新我的答案。