stat_ellipse 和 geom_point 使用 Color Brewer 的颜色相同

问题描述 投票:0回答:1

我希望每个组都有省略号(

Pond
)以匹配来自
R brewer
,“
YlOrRd
”的点颜色分配。我希望椭圆轮廓为黑色,但所有椭圆都填充为黑色。

plot <- ggplot(plotpca, aes(x=PC1,y=PC2)) +
  geom_point(aes(fill=Pond),pch=21,size=3) +
  stat_ellipse(geom = "polygon",aes(group=Pond,color=Pond),alpha = 0.25, color="black") +
  scale_fill_brewer(palette="YlOrRd",aesthetics = c("color", "fill")) 
plot(plot)

给我:

这是我要绘制的 df 的子集:

> dput(head(plotpca))
structure(list(PC1 = c(-0.0505964065450834, 0.0546247180570643, 
0.0385794513330037, -0.0421393543493591, 0.0284612585699651, 
0.0209291807114026), PC2 = c(0.0534548597921124, 0.00385797355835037, 
0.0217307675631555, -0.0319287619859987, -0.00204735494019053, 
-0.00709853162293151), group = structure(c(5L, 1L, 1L, 1L, 1L, 
1L), levels = c("LIL", "RHM", "SCS", "STN", "STS", "TS"), class = "factor"), 
    Pond = structure(c(6L, 4L, 4L, 4L, 4L, 4L), levels = c("RHM", 
    "TS", "SCS", "LIL", "STN", "STS"), class = "factor"), name = c("Ga0598233", 
    "Ga0598239", "Ga0598240", "Ga0598241", "Ga0598242", "Ga0598243"
    )), percentVar = c(0.570804459528504, 0.31299732413872), row.names = c("Ga0598233", 
"Ga0598239", "Ga0598240", "Ga0598241", "Ga0598242", "Ga0598243"
), class = "data.frame")
r ggplot2 colors ellipse colorbrewer
1个回答
0
投票

您的代码非常接近,您只需将

fill = Pond
添加到
stat_ellipse()

的映射中
plot <- ggplot(plotpca, aes(x=PC1,y=PC2)) +
      geom_point(aes(fill=Pond),pch=21,size=3) +
      stat_ellipse(geom = "polygon",aes(group=Pond,color=Pond,fill=Pond),alpha = 0.25, color="black") +
      scale_fill_brewer(palette="YlOrRd",aesthetics = c("color", "fill")) 
    plot(plot)
© www.soinside.com 2019 - 2024. All rights reserved.