简体   繁体   中英

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. 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

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

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.

This roughly replicates what is going on in ggord . Looking at the source for the package, the ellipses are implemented differently in ggord than below, hence the small differences. If that is a big deal you can review the source and make changes. By default, geom_point doesn't have a fill attribute. So we set the shapes to a character type that does, and then specify color = 'black' in geom_point() . 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"
  ) 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM