Add a horizontal line to plot and legend in ggplot2

后端 未结 2 1041
甜味超标
甜味超标 2020-12-01 05:06

This code creates a nice plot but I would like to add a horizontal black line at y=50 AND have the legend show a black line with the text \"cutoff\" in the legend, but leave

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 06:06

    Another solution :

    gg <- ggplot(the.data, aes( x = year, y = value ) ) + 
            geom_point(aes(colour = source)) + 
            geom_smooth(aes(group = 1))
    
    cutoff <- data.frame(yintercept=50, cutoff=factor(50))
    gg + 
      geom_hline(aes(yintercept=yintercept, linetype=cutoff), data=cutoff, show_guide=TRUE) 
    

    This code generates exactly the same graphic as the one in point (1) of @G. Grothendieck. But it is more easy to adapt to graphics with several layers.

提交回复
热议问题