R 在ggplot中添加点到比例图例

R 在ggplot中添加点到比例图例,r,ggplot2,R,Ggplot2,我有一组简单的点放在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(r

我有一组简单的点放在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
这是一个相当不错的情节,但是在导航蓝测试点的图例中没有任何内容

有人知道如何将测试点添加到图例输出中吗?通过将第一个
geom_点
更改为

    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_点()
来完成此操作。我希望能够在不将新数据与其他数据合并的情况下为比例图例添加单独的值。

我认为,使用多个图层的唯一原因是尺寸,这对我来说似乎是不必要的

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")

new_df以下是我的尝试。如果你想用你描述的方式绘制图形,你可以这样做。您缺少海军蓝的原因是您没有在aes()中为
测试点
添加颜色。我使用
transform()
创建了一个名为
hue
的新列。然后,我添加了
scale\u color\u 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"))

test\u point我正在寻找一种方法,通过添加另一个
geom\u point()
来实现这一点。我可能已经合并了data.frames,但我正在尝试确定如何使用附加元素进行合并。不过,谢谢你的想法。
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"))