【发布时间】:2021-08-27 15:29:30
【问题描述】:
我正在尝试创建一个组合图,其中包括所有点的geom_point 和使用geom_encircle 围绕数据组的多边形。但是,我只希望包围特定的群体。下面我有一些示例代码可以帮助说明。
x <- c(10, 12, 4, 18, 6, 9, 2, 2, 7, 23, 13, 13, 11, 6, 22)
y <- c(3, 2, 12, 15, 23, 20, 6, 21, 6, 8, 15, 19, 10, 18, 14)
group <- c("a", "b", "b", "b","b","b","b", "c", "c", "c","c","c", "c", "d", "e")
class <- c(NA, "1", "1","1","1","1","1","2","2","2","2","2","2", NA, NA)
df <- as.data.frame(cbind(x,y,group,class))
df$x <- as.numeric(df$x)
df$y <- as.numeric(df$y)
library(ggplot2)
library(ggalt)
ggplot(df, aes(x, y)) +
geom_point(aes(color = group)) +
geom_encircle(aes(fill = class), s_shape = 1, expand = 0,
alpha = 0.2, color = "black", na.rm = TRUE, show.legend = FALSE)
下图是我得到的,但是,我不想要灰色三角形,只想要蓝色和红色的形状。我认为设置na.rm = TRUE 会删除geom_encircle 的那些行,但它不会(我假设NA 需要在x 或y 列中)。我也尝试了一些尝试对数据进行子集化,但是我未能成功保留点但删除了形状。
【问题讨论】:
标签: r ggplot2 polygon na geom-point