2015-03-31 78 views
1

我在R中有以下數據集,我想在散點圖中繪製。R scatterplot,傳說中沒有顯示

 user distance time 
1 1 8.559737 4 
2 1 5.013872 5 
3 1 11.168995 9 
4 1 4.059428 4 
5 1 3.928071 4 
6 1 12.403195 7 

我使用以下R代碼生成我的圖。

plot <- ggplot(scatter, aes(x=scatter[['distance']], y=scatter[['time']])) + 
      geom_point(shape=16, size=5, colour=scatter[['user']]) + 
      scale_x_continuous("Distance", limits=c(0,100), breaks=seq(0, 100, 10)) + 
      scale_y_continuous("Time", limits=c(0,20), breaks=seq(0, 20, 2)) 

png(filename="scatters/0_2_scatter.png", width=800, height=800) 
plot(plot) 
dev.off() 

這會產生以下結果。 enter image description here

爲什麼我的圖例不顯示? geom_point中沒有定義顏色是否足夠? 我正在嘗試生成一個包含黑點和文本'user1'的圖例。

回答

3

嘗試:

ggplot(scatter, aes(x=distance, y=time)) + 
      geom_point(shape=16, size=5, mapping = aes(colour=user)) + 
      scale_x_continuous("Distance", limits=c(0,100), breaks=seq(0, 100, 10)) + 
      scale_y_continuous("Time", limits=c(0,20), breaks=seq(0, 20, 2)) 

data論點從aes()規格單獨的全部目的是ggplot做非標評價讓你僅指(不帶引號)列名。請勿專門通過aes()內部的$[[[列提及。

當您的圖案地圖美學(即使用aes())時,圖例應該出現,您不用於顏色。