How do I set the color of a single line using ggplot

前端 未结 2 461
太阳男子
太阳男子 2021-01-22 08:17
df<-data.frame(adjuster=c(\"Mary\",\"Mary\",\"Bob\",\"Bob\",\"Mary\"), 
           date=as.Date(c(\"2012-1-1\",\"2012-2-1\",\"2012-3-1\",\"2012-4-1\",\"2012-5-1\")),          


        
2条回答
  •  暖寄归人
    2021-01-22 09:09

    This way Paid column is added as a separate layer, but that means it's not in the legend. I don't get why the column Paid would go in a legend with different levels of adjuster anyway. Why is that?

    library(ggplot2)
    df <- data.frame(adjuster=c("Mary","Mary","Bob","Bob","Mary"), 
               date=as.Date(c("2012-1-1","2012-2-1","2012-3-1","2012-4-1","2012-5-1")), 
               total=c(10,15,25,15,20), paid=c(5,5,10,10,15))
    
    ggplot(df, aes(x=date,y=total,color=adjuster,group=1)) + 
      geom_point() +
      geom_line(aes(x=date, y=total, color=adjuster, group=1)) +
      geom_line(aes(x=date, y=paid, group=2), data=df, colour="black") +
      scale_color_discrete(name="Legend")
    

    enter image description here

提交回复
热议问题