【问题标题】:R ggplot changing the color and legend sequences in legend [duplicate]R ggplot改变图例中的颜色和图例序列[重复]
【发布时间】:2019-01-11 04:22:21
【问题描述】:

这篇文章中的数据:joining all points inside a grouped point plot using ggplot 保持不变我已经添加了一列。在 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))

现在剧情如下

你能帮我解决这个问题吗

【问题讨论】:

  • 你有什么问题?
  • 查看图例并绘制红色显示名称绿色和绿色显示红色。请参阅“Comp_apply”命令绘制它应该是红色的
  • 搜索 [ggplot2] wrong colors 以获取有关如何正确分配颜色的十几个问题。

标签: r ggplot2 legend line-plot


【解决方案1】:

如果您想为整个geom 分配颜色,只需将colour="red" 放在aes() 之外。但是颜色不会出现在图例中,因为它没有映射到任何因素。

执行此操作的正确方法是修改您的data.frame,使Comp_applyL1_25 显示为具有相同因素的两种模式(即在同一列中)。由于未提供您的数据,我无法为您完成。

那么您只需调用一次geom_pointgeom_line。提供数据,我会更新我的答案。

【讨论】:

  • 是的,这帮助我将 color="red" 放在 aes() 之外
猜你喜欢
  • 1970-01-01
  • 2017-01-16
  • 2021-02-07
  • 1970-01-01
  • 2016-03-22
  • 1970-01-01
  • 2021-10-29
  • 2022-06-22
  • 2013-09-02
相关资源
最近更新 更多