2016-11-07 64 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