我有一组简单的点放在R的散点图中。一组点是训练数据,另一组是单点测试数据。我制作了训练数据的散点图,并将测试数据点添加到同一个图中,所有这些都使用ggplot2()
。我希望将测试数据点添加到已为训练数据定义的相同图例中。
首先,为那些想要一起玩的人提供一些数据。
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()
执行此操作。我希望能够在不将新数据与其他数据合并的情况下为比例图例添加单独的值。
答案 0 :(得分: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")
答案 1 :(得分:0)
这是我的尝试。如果你想以你描述的方式绘制ggplot数字,你可以这样做。你缺少深蓝色的原因是你没有在test_point
的aes()中添加颜色。我使用hue
创建了一个名为transform()
的新列。然后,我添加了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"))