绘制单个geom_smooth曲线而不是多个

时间:2017-12-19 09:10:45

标签: r ggplot2 categories

我正在绘制一个由不同颜色表示的3个不同类别的图表。我想要一条曲线来表示总数据的趋势,但是当我使用geom_smooth时,我得到3条曲线,每条曲线一条曲线。我的代码是:

IF { FIXED [Customerid], [Certain_City] : MIN(Order_Date) < [Certain_Date] }
AND { FIXED [Customerid], [Certain_City] : MAX(Order_Date) >= [Certain_Date]}
THEN "Customer Ordered from City before"
ELSE "Customers first Order from this City"
END

2 个答案:

答案 0 :(得分:3)

有两种方法可以解决这个问题: 1)覆盖geom_smooth图层中的颜色aestetic

   #plot the data
   ggplot(data=transfer_data, 
          mapping=aes(x=DATE_OF_TRANSFER, 
                      y=NUMBER_OF_TRANSFERS, 
                      colour = region)) + 
    geom_point() + 
    geom_smooth(color="black") + 
    scale_colour_manual(values=c("green", "blue", "red", "orange"))

或 2)仅将颜色aestetic移动到需要它的层

   #plot the data
   ggplot(data=transfer_data, 
          mapping=aes(x=DATE_OF_TRANSFER, 
                      y=NUMBER_OF_TRANSFERS)) + 
    geom_point(mapping=aes(colour = region)) + 
    geom_smooth() + 
    scale_colour_manual(values=c("green", "blue", "red", "orange"))

答案 1 :(得分:1)

您应该使用:

library(ggplot2)
ggplot(transfer_data, aes(DATE_OF_TRANSFER, NUMBER_OF_TRANSFERS)) + 
    geom_point(aes(color = region)) + 
    geom_smooth() + 
    scale_colour_manual(values = c("green", "blue", "red", "orange"))
  • 当您指定:ggplot(data=transfer_data, aes(x=DATE_OF_TRANSFER, y=NUMBER_OF_TRANSFERS, colour = region))时,您要求geom_pointgeom_smooth都应由region着色。
  • 指定:geom_point(aes(color = region)) + geom_smooth()时,要求所有区域的区域和平滑线都是相同的。