geom_point() and geom_line() for multiple datasets on same graph in ggplot2

前端 未结 2 1583
囚心锁ツ
囚心锁ツ 2020-12-13 04:10

I\'m trying to plot three datasets onto the same graph. One dataset should appear on the graph as just a set of unconnected points, whereas the other two should appear as co

相关标签:
2条回答
  • 2020-12-13 04:50

    The trick is that each layer can have its own dataset. So you have to subset the data to exclude L1=="p1" from the data provided to geom_line:

    ggplot(zz, aes(x, y=value, color=L1)) + 
      geom_point() + 
      geom_line(data=zz[zz$L1!="p1", ]) +
      scale_color_manual("Dataset", 
           values = c("p1" = "darkgreen", "p2" = "blue", "p3" = "red"))
    

    enter image description here

    0 讨论(0)
  • 2020-12-13 04:58

    You can feed a different dataset into each geom. So you can pass in a dataset excluding p1 into the geom_line layer. Something like this should work:

    ggplot(zz, aes(x, value, color = L1)) + 
      geom_point() + 
      geom_line(data = subset(zz, L1 %in% c("p2", "p3")), aes(group = L1)) +
      scale_color_manual("Dataset", values = c("p1" = "darkgreen", "p2" = "blue", "p3" = "red")) 
    
    0 讨论(0)
提交回复
热议问题