2017-10-11 194 views
-4

我想繪製一些數據(「DAPC2」),其中兩個變量鏈接到ggplot2中的座標。數據是這樣的:更改顏色並在ggplot中添加圖例

  LD1  LD2  Locality Ecoregion 
CA2 0.9524254 -15.906715 Caldera Central_Chile 
CO4 11.4640606 3.644242 Cocholgue Araucanian 
HU2 -17.3216357 10.577911 Huinay  Chiloense 
HU4 -17.9015095 10.813084 Huinay  Chiloense 
LH1 2.5713149 -17.984544 Herradura Central_Chile 

而且到目前爲止我的代碼是這樣的:

myPlot <- ggplot(DAPC2, aes(x=DAPC2$LD1, y=DAPC2$LD2)) 
myPlot + theme_bw() + theme(panel.border=element_blank(), panel.grid.major=element_blank(), panel.grid.minor=element_blank(), axis.line=element_line(colour="black")) + geom_point(alpha=0.3, col=as.integer(DAPC2$popnames.Locality), pch=as.integer(DAPC2$popnames.Ecoregion)+14, cex=6) 

一方面,我試圖改變顏色的調色板,但是我不能這樣做給定整數向量。此外,我試圖包含一個顯示兩個變量的圖例(即Locality和Ecoregion)。有什麼建議?

+2

不需要在'ggplot'的'aes'中包含'DAPC2 $'。 – Jaap

回答

0

要將顏色/形狀添加到ggplot中的點,您需要添加顏色和形狀美學。

嘗試

ggplot(DAPC2, aes(x=DAPC2$LD1, y=DAPC2$LD2, colour=Locality, shaoe=Ecoregion))

,並從你的代碼的其餘部分刪除,如果生態區域和局部性的所有實例。這也將增加顏色和形狀的圖例。

1

對於圖例部分,您可以爲aes部分添加擴展名。

如果你想有一個傳說:

myPlot <- ggplot(DAPC2, aes(x=DAPC2$LD1, y=DAPC2$LD2)) 

更改爲:

myPlot <- ggplot(DAPC2, aes(x=DAPC2$LD1, y=DAPC2$LD2, fill = DAPC2$Locality)) 

如果你想同時,你可以做facet_wrap這將有一個傳說爲每個不同的地塊沿生態區。

myPlot <- ggplot(DAPC2, aes(x=DAPC2$LD1, y=DAPC2$LD2, fill = DAPC2$Locality), facet_wrap(~DAPC2$Ecoregion)) 
相關問題