2015-10-14 134 views
0

我有一組簡單的點放置在R的散點圖中。一組點是訓練數據,另一組點是測試數據的單點。我製作了訓練數據的散點圖,並將測試數據點添加到同一圖中,全部使用ggplot2()。我希望將測試數據點添加到已爲訓練數據定義的相同圖例。在ggplot中添加點縮放圖例

首先,一些數據適合那些想玩的人。

A1 <- c(0,0) 
A2 <- c(1,1) 
A3 <- c(2,2) 
B1 <- c(6,6) 
B2 <- c(5.5,7) 
B3 <- c(6.5,5) 
train1 <- data.frame(rbind(A1,A2,A3, B1,B2,B3)) 
names(train1) <- c("X", "Y") 
test_point <- data.frame("X" = 4.0, "Y" = 4.0) # make the test point a df to place nicely with ggplot. 
cl <- factor(c(rep("A",3),rep("B",3))) # class labels 

現在讓我們把火車和測試數據的散點圖:

ggplot(data = train1) + 
    aes(X, Y, colour = cl) + 
    geom_point(size = 3) + 
    geom_point(data = test_point, aes(X, Y), colour = "NavyBlue", size = 4) + 
    labs(size= "1", x = "X coords", y = "Y coords", 
     title = "Features for KNN", vjust=-10, 
     colour = "Class Labels") + # change the label for legend by variable name in aes() 
    theme(axis.text=element_text(size=16), 
      axis.text.x = element_text(angle=0, vjust=1), 
      axis.title=element_text(size=16), 
      legend.position="bottom", legend.direction = "vertical", #change location and direction of legend 
      legend.text = element_text(colour="blue", size = 16, face = "bold")) + #change style for legend text 
    theme(plot.title = element_text(size = 18)) 

這使得一個足夠體面的情節,但沒有圖例爲NavyBlue test_point什麼。

Scatterplot with nothing in legend for test-point

任何人都知道如何將test_point添加到傳奇輸出?我已經得到它來代表,但不是很好的,通過改變第一至geom_point

geom_point(data = test_point, aes(X, Y, colour = "Test Data"), size = 4) + 

其產生此沒有​​被指定NavyBlue。那麼如何用這個配方來保持海軍藍色呢?

Legend has color name as name

天真,一個可以嘗試

geom_point(data = test_point, aes(X, Y, colour = "Test Data"), colour = "NavyBlue", size = 4) + 

但結果在第一個情節,即額外的圖例項消失。

編輯:爲了清楚,我試圖用一個單獨的geom_point()做到這一點。我希望能夠將一個單獨的值添加到比例圖例中,而不將該新數據與其他數據合併。

回答

0

在我看來,你使用多個圖層的唯一原因是尺寸,這對我來說似乎沒有必要。

new_df <- rbind(train1, test_point) 
new_df$size <- c(rep(3,6), 4) 
new_df$cl <- c(rep("red", 3), rep("green", 3), "NavyBlue") 

ggplot(new_df, aes(X,Y, colour= cl)) + geom_point(size= 3) + geom_point(aes(size= new_df$size)) + 
     scale_color_manual(values= c("red", "NavyBlue", "green")) + 
    labs(size= "1", x = "X coords", y = "Y coords", 
     title = "Features for KNN", vjust=-10, 
     colour = "Class Labels") + # change the label for legend by variable name in aes() 
    theme(axis.text=element_text(size=16), 
     axis.text.x = element_text(angle=0, vjust=1), 
     axis.title=element_text(size=16), 
     legend.position="bottom", legend.direction = "vertical", #change location and direction of legend 
     legend.text = element_text(colour="blue", size = 16, face = "bold")) + #change style for legend text 
    theme(plot.title = element_text(size = 18)) + 
    scale_size(guide= "none") 

enter image description here

+0

我尋求一種方法來做到這一點增加了另外'geom_point()'。我可能已經合併了data.frames,但我正在嘗試確定如何使用其他元素來完成此操作。不過謝謝你的想法。 –

0

這裏是我的嘗試。如果你想用你描述的方式繪製ggplot數字,你可以這樣做。你錯過海軍藍的原因是你沒有把顏色放在aes()中爲test_point。我使用transform()創建了一個名爲hue的新列。然後,我添加了scale_color_manual()部分來安排圖例。

test_point <- transform(test_point, hue = "Navy blue") 

ggplot(data = train1) + 
aes(X, Y, colour = cl) + 
geom_point(size = 3) + 
geom_point(data = test_point, aes(X, Y, colour = hue), size = 4) + 
labs(size= "1", x = "X coords", y = "Y coords", 
    title = "Features for KNN", vjust=-10, 
    colour = "Class Labels") + # change the label for legend by variable name in aes() 
theme(axis.text=element_text(size=16), 
     axis.text.x = element_text(angle=0, vjust=1), 
     axis.title=element_text(size=16), 
     legend.position="bottom", legend.direction = "vertical", #change location and direction of legend 
     legend.text = element_text(colour="blue", size = 16, face = "bold")) + #change style for legend text 
theme(plot.title = element_text(size = 18)) + 
scale_color_manual(values=c("red", "green", "navy blue"), 
        name="Class labels", 
        breaks = c("A", "B", "Navy blue"), 
        labels=c("A", "B", "Navy blue")) 

enter image description here