我有一组简单的点放置在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什么。
任何人都知道如何将test_point
添加到传奇输出?我已经得到它来代表,但不是很好的,通过改变第一至geom_point
geom_point(data = test_point, aes(X, Y, colour = "Test Data"), size = 4) +
其产生此没有被指定NavyBlue
。那么如何用这个配方来保持海军蓝色呢?
天真,一个可以尝试
geom_point(data = test_point, aes(X, Y, colour = "Test Data"), colour = "NavyBlue", size = 4) +
但结果在第一个情节,即额外的图例项消失。
编辑:为了清楚,我试图用一个单独的geom_point()
做到这一点。我希望能够将一个单独的值添加到比例图例中,而不将该新数据与其他数据合并。
我寻求一种方法来做到这一点增加了另外'geom_point()'。我可能已经合并了data.frames,但我正在尝试确定如何使用其他元素来完成此操作。不过谢谢你的想法。 –