【问题标题】:How to plot filled points and confidence ellipses with the same color using ggplot in R?如何使用 R 中的 ggplot 绘制具有相同颜色的填充点和置信椭圆?
【发布时间】:2022-01-14 17:50:22
【问题描述】:

我想从判别函数分析中绘制一个图表,其中点必须有黑色边框并用特定颜色填充,置信椭圆必须与填充点的颜色相同。使用下面的代码,我几乎得到了我想要的图形,除了点没有黑色边框:

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

我可以使用以下代码在点上设置黑色边框,但随后置信椭圆变为黑色。

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

我想保留第一个图中的椭圆,但保留第二个图中的点。但是,我无法弄清楚我该如何做到这一点。如果有人对如何做到这一点有建议,我将不胜感激。我正在使用“ggord”包,因为我学会了如何使用它来运行分析,但是如果有人对如何仅使用 ggplot 执行相同的操作有任何建议,那就没问题了。

【问题讨论】:

    标签: r ggplot2 plot


    【解决方案1】:

    这大致复制了ggord 中的情况。查看包的源代码,ggord 中的省略号与下面的不同,因此差异很小。如果这很重要,您可以查看源并进行更改。默认情况下,geom_point 没有填充属性。所以我们将形状设置为字符类型,然后在geom_point() 中指定color = 'black'。完整代码(包括投影原始数据)如下。

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

    要绘制箭头,您可以从输出中获取缩放比例并使用geom_segment 绘制它。我使用了颜色/alpha,因此它们在下面的图中可见。

    scaling <- data.frame(linear$scaling)
    
    ...
      geom_segment(data = scaling,
                   aes(x = 0,
                       y = 0,
                       xend = LD1,
                       yend = LD2),
                   arrow = arrow(),
                   color = "black") +
      geom_text(data = scaling,
                aes(x = ifelse(LD1 <= 0.1, LD1 - 2, LD1 + 2),
                    y = ifelse(LD2 <= 0.1, LD2 - 1, LD2 + 1)),
                label = rownames(scaling),
                color = "black") +
    ...
    

    【讨论】:

    • 非常感谢!还有一个问题 - 我如何在此图中绘制负载箭头?在上面的代码中使用 ggord,我可以删除labcol = "transparent", arrow = NULL,然后我会默认获得箭头。我通常不会在最终图表中保留箭头,但它们对于解释分析很有用。
    • @Davidgonzalez81 查看编辑。
    猜你喜欢
    • 1970-01-01
    • 2020-10-03
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2016-07-09
    • 2021-06-17
    相关资源
    最近更新 更多