2016-06-14 29 views
0

我使用ggplot與ggmap和以下是我使用的命令,指定geompoint顏色變量 - ggplotř

print(ggmap(m) + 
     geom_point(aes(x=ga_long, y=ga_lat, color = variable1, size = size) , data=il) + 
     scale_size_continuous(range = c(1,5)) + 
     xlab("Latitude") + ylab("Longitude") 
     # + scale_colour_continuous(c("green", "black", "red")) 
) 

這裏的顏色,我已經給變量1。它似乎工作正常,但我想指定的顏色,因爲我不熟悉現有的顏色。如果變量1中有三個因素,我想給出綠色,黑色,紅色作爲特定因素的組合。我試過以下,

print(ggmap(m) + 
      geom_point(aes(x=ga_long, y=ga_lat, color = 
          ifelse(variable1 == 0, 'green', 
            ifelse(variable1 == 1, 'black', 'red')), size = size), data=il) + 
      scale_size_continuous(range = c(1,5)) + 
      xlab("Latitude") + ylab("Longitude") 
      # + scale_colour_continuous(c("green", "black", "red")) 
    ) 

但是這一個沒有幫助。

有人可以幫我做這件事嗎?

謝謝

+0

使用我們的第一個代碼'scale_colour_manual'和值設置爲你的顏色 –

+0

@RichardTelford我想這一點,scale_colour_manual(C(「綠色」,「黑」,「紅」),我得到,錯誤f(...):參數「值」丟失,沒有默認值。不知道我在這裏做了什麼錯誤。 – haimen

+1

設置'values = c(「green」,「black」,「red」) ' –

回答

1

我覺得你很接近。嘗試:

print(ggmap(m) + 
     geom_point(aes(x=ga_long, y=ga_lat, color = variable1, size = size) , data=il) + 
     scale_size_continuous(range = c(1,5)) + 
     xlab("Latitude") + ylab("Longitude") + 
     scale_color_manual(values=c("green", "black", "red")) 
)