2016-11-07 48 views
0

如何在此代码中添加图例?在ggplot2上制作图例

谢谢!

ggplot()+ 
    geom_point(data=avg_harv_df, aes(x=samp_per, y=ndvi), size=3, color='red') + 
    geom_point(data=avg_sjer_df, aes(x=samp_per, y=ndvi), size=3, color='blue') + 
    ylab("NDVI")+ 
    xlab("Sampling period") 

回答

0

选项1:

ggplot()+ 
    geom_point(data=avg_harv_df, aes(x=samp_per, y=ndvi, col = "a"), size=3) + 
    geom_point(data=avg_sjer_df, aes(x=samp_per, y=ndvi, col = "b"), size=3) + 
    ylab("NDVI")+ 
    xlab("Sampling period") 

选项2:

combined <- dplyr::bind_rows(avg_harv_df, avg_sjer_df, .id = 'col') 
ggplot(combined, aes(x=samp_per, y=ndvi, col = col))+ 
    geom_point(size=3) + 
    ylab("NDVI")+ 
    xlab("Sampling period") 

我几乎总是喜欢选项2

+0

谢谢,斧头兵!现在我有了我的传奇,但是如果我想用不同于颜色的其他名称命名我的传奇(例如网站),该怎么办? –

+0

10s google http://stats.stackexchange.com/q/5007/114900 – Axeman