Different legend-keys inside same legend in ggplot2

前端 未结 2 1582
北恋
北恋 2020-12-03 14:38

Let\'s say I don\'t need a \'proper\' variable mapping but still would like to have legend keys to help the chart understanding. My actual data are similar to the following

2条回答
  •  鱼传尺愫
    2020-12-03 15:02

    I am not aware of any way to do this easily, but you can do a hack version like this (using your melted dataframe):

    p <- ggplot(df.m, aes(id, value)) +
      geom_line(aes(colour = variable, linetype = variable)) + scale_linetype_manual(values = c(1,0)) +
      geom_point(aes(colour = variable, alpha = variable)) + scale_alpha_manual(values = c(0,1))
    

    enter image description here

    The key is that you need to get the mapping right to have it displayed correctly in the legend. In this case, getting it 'right', means fooling it to look the way you want it to. It's probably worth pointing out this only works because you can set linetype to blank (0) and then use the alpha scale for the points. You can't use alpha for both, because it will only take one scale.

提交回复
热议问题