Why ggplot2 legend not show in the graph

后端 未结 1 907
伪装坚强ぢ
伪装坚强ぢ 2021-01-12 17:35

I use ggplot to scatterplot 2 datasets and want to show the legend in the top left. I tried some code but didn\'t work. I am not sure why this happened.

ggpl         


        
相关标签:
1条回答
  • 2021-01-12 17:41

    Since values were not provided, I have used my own values for the demonstration purpose.

    mf is a dataframe with log and val as it's column.

    You need to put the color parameter inside the aesthetics. This will result in the mapping of colors for the legend. After that you can manually scale the color to get any color you desire.

    you can use the below code to get the desired result.

    ggplot(mf, aes(val,log))+
        geom_point(aes(color = "Dataset1"))+
        geom_point(data=mf2,aes(color="Dataset2"))+
        labs(colour="Datasets",x="xxx",y="yyy")+
        theme(legend.position = c(0, 1),legend.justification = c(0, 1))+
        scale_color_manual(values = c("blue","red"))
    

    0 讨论(0)
提交回复
热议问题