How to insert ellipses with color opacity in groups?

170 views Asked by At

I have this code:

library(echarts4r)

iris |> 
  dplyr::group_by(Species) |> 
  e_charts(Sepal.Length) |> 
  e_scatter(Petal.Length, symbol_size = 5) |>
  e_tooltip() 

How to insert ellipses with color opacity in groups?

Something like:

enter image description here

1

There are 1 answers

0
TristanU On

You can use the stat_ellipse function:

We can use this example to perform a Linear Discriminant Analysis in R, similar to yours and easy to reproduce with the code here.

Then, you can use ggplot and stat_ellipse function, as already suggested,

and to vary the opacity of the ellipse's color as you wish, you can change the alpha value, 1 for very opaque and 0 for transparent,

ggplot(lda_plot, aes(LD1, LD2)) +
  geom_point(aes(color = Species)) + 
  stat_ellipse(geom="polygon", aes(fill = Species), 
               alpha = 0.2,
               show.legend = FALSE, 
               level = 0.95) +
  ggtitle("a) Opacity 0.2")

ggplot(lda_plot, aes(LD1, LD2)) +
  geom_point(aes(color = Species)) + 
  stat_ellipse(geom="polygon", aes(fill = Species), 
               alpha = 0.8,
               show.legend = FALSE, 
               level = 0.95) +
  ggtitle("b) Opacity 0.8")

such as, enter image description here

I hope I have answered your question