2015-10-14 70 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