简体   繁体   English

如何在 R 中使用 ggplot 填充具有相同颜色的点和置信椭圆 plot?

[英]How to plot filled points and confidence ellipses with the same color using ggplot in R?

I would like to plot a graph from a Discriminant Function Analysis in which points must have a black border and be filled with specific colors and confidence ellipses must be the same color as the points are filled.我想 plot 来自判别式 Function 分析的图表,其中点必须有黑色边框并填充特定的 colors 和置信椭圆必须填充相同的颜色。 Using the following code, I get almost the graph I want, except that points do not have a black border:使用下面的代码,我几乎得到了我想要的图形,除了点没有黑色边框:

library(ggplot2)
library(ggord)
library(MASS)

data("iris")

set.seed(123)
linear <- lda(Species~., iris)
linear

dfaplot <- ggord(linear, iris$Species, labcol = "transparent", arrow = NULL, poly = FALSE, ylim = c(-11, 11), xlim = c(-11, 11))
dfaplot +
  scale_shape_manual(values = c(16,15,17)) +
  scale_color_manual(values = c("#00FF00","#FF00FF","#0000FF")) +
  theme(legend.position = "none")

PLOT 1 PLOT 1

I could put a black border on the points by using the following code, but then confidence ellipses turn black.我可以使用以下代码在点上添加黑色边框,但随后置信椭圆变为黑色。

dfaplot +
  scale_shape_manual(values = c(21,22,24)) +
  scale_color_manual(values = c("black","black","black")) +
  scale_fill_manual(values = c("#00FF00","#FF00FF","#0000FF")) +
  theme(legend.position = "none")

PLOT 2 PLOT 2

I would like to keep the ellipses as in the first graph, but the points as in the second one.我想保留第一个图中的椭圆,但保留第二个图中的点。 However, I am being unable to figure out how I could do this.但是,我无法弄清楚如何做到这一点。 If anyone has suggestions on how to do this, I would be very grateful.如果有人对如何做到这一点有建议,我将不胜感激。 I am using the "ggord" package because I learned how to run the analysis using it, but if anyone has suggestions on how to do the same with only ggplot, it would be fine.我正在使用“ggord”package,因为我学会了如何使用它来运行分析,但是如果有人对如何仅使用 ggplot 执行相同的操作有任何建议,那就没问题了。

This roughly replicates what is going on in ggord .这大致复制了ggord中发生的事情。 Looking at the source for the package, the ellipses are implemented differently in ggord than below, hence the small differences.查看 package 的源代码,椭圆在ggord中的实现方式与下面的不同,因此差异很小。 If that is a big deal you can review the source and make changes.如果这很重要,您可以查看源并进行更改。 By default, geom_point doesn't have a fill attribute.默认情况下, geom_point没有填充属性。 So we set the shapes to a character type that does, and then specify color = 'black' in geom_point() .所以我们将形状设置为字符类型,然后在geom_point()中指定color = 'black' The full code (including projecting the original data) is below.完整代码(包括投影原始数据)如下。

set.seed(123)
linear <- lda(Species~., iris)
linear

# Get point x, y coordinates
df <- data.frame(predict(linear, iris[, 1:4]))
df$species <- iris$Species

# Get explained variance for each axis
var_exp <- 100 * linear$svd ^ 2 / sum(linear$svd ^ 2)

ggplot(data = df,
       aes(x = x.LD1,
           y = x.LD2)) +
  geom_point(aes(fill = species,
                 shape = species),
             size = 4) +
  stat_ellipse(aes(color = species),
               level = 0.95) +
  ylim(c(-11, 11)) +
  xlim(c(-11, 11)) +
  ylab(paste("LD2 (",
             round(var_exp[2], 2),
             "%)")) +
  xlab(paste("LD1 (",
             round(var_exp[1], 2),
             "%)")) +
  scale_color_manual(values = c("#00FF00","#FF00FF","#0000FF")) +
  scale_fill_manual(values = c("#00FF00","#FF00FF","#0000FF")) +
  scale_shape_manual(values = c(21, 22, 24)) +
  coord_fixed(1) +
  theme_bw() +
  theme(
    legend.position = "none"
  ) 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据置信区间 R 对 ggplot 2 中的点进行着色 - How to color points in ggplot 2 based on confidence interval R 如何在R中的PCA图中添加多个置信椭圆? - How to add multiple confidence ellipses to PCA plot in R? 如何在R中绘制半填充点(最好使用ggplot) - How to draw half-filled points in R (preferably using ggplot) 使用R中的ggplot2在散点图中指向具有单个椭圆的点 - Points in a scatterplot with individual ellipses using ggplot2 in R R / ggplot2 /如何从散点图中的连接点移动到填充和透明三角形? - R/ ggplot2/ how to move from connected points in a scatter plot to filled and transparent triangles? 如何使用 R plotly 在一个图上绘制多个填充的置信带 - How to draw multiple filled confidence bands on one plot using R plotly R ggplot:置信区间图 - R ggplot: confidence interval plot 在ggplot R中使用边框可自定义pch时,如何使geom_errorbar()与点的填充颜色相同? - How to make geom_errorbar() the same color as the fill of points when using border customizable pch in ggplot R? 如何使用 ggplot 在 r 中的 PCA 图上标记特定数据点 - How to label specific data points on a PCA plot in r using ggplot 如何在 ggplot 中用彩色边框填充 plot 点(白黑对比) - How to plot filled points with multicolored borders in ggplot (white black contrast)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM