Skip to content

geom_mark_ellipse renders incorrect ellipse orientation with 3-point groups #354

Open

Description

@Radio-Controlled

geom_mark_ellipse renders ellipses with a wrong orientation for certain 3-point group configurations. The ellipse appears to have the correct shape but is displaced/mirrored vertically, suggesting a sign error in the coordinate transformation rather than a failure of the Khachiyan algorithm itself.

This may be related to issue #214?

Reproducible example:

library(ggplot2)
library(ggforce)

bug_data <- data.frame(
 PC1 = c(-1.33, 92.6, 78.5),
 PC2 = c(46.5, -22.4, 0.9)
)

ggplot(bug_data, aes(PC1, PC2)) +
 geom_point(size = 3) +
 ggforce::geom_mark_ellipse(aes(fill = "D"), alpha = 0.3)

# expanded view to draw whole ellipse
ggplot(bug_data, aes(PC1, PC2)) +
  geom_point(size = 3) +
  ggforce::geom_mark_ellipse(aes(fill = "D"), alpha = 0.3) +
  coord_cartesian(xlim = c(-50, 150), ylim = c(-100, 100))  
Image

A quick manual Khachiyan implementation (made with Claude) shows an ellipse that correctly encloses all points and is indeed a vertical mirror of the geom_mark_ellipse ellipse.
Image

Session info
R: 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
ggforce_0.5.0
ggplot2_4.0.1

Activity

  1. SotiriosVasileiadis commented on Feb 12, 2026

    @SotiriosVasileiadis

    not sure if it helps... cursor built a functional (at least for my so far run plots) version of the "mark_ellipse.R" script found in the https://github.com/thomasp85/ggforce/tree/main/R.

    Please test for proper functionality. My tests were not exhaustive and I am not fully familiar with ellipse functions.

    Could not upload the R extension file so I used a txt extension. You will find it here:
    mark_ellipse.txt

    If you want to test you can
    -> clone the https://github.com/thomasp85/ggforce repository in your computer
    -> replace the mark_ellipse.R with the linked mark_ellipse.txt
    -> change extension from txt to R
    -> install the local repo in R with

    devtools::install("my/local/ggforce_folder") 

    -> test with the following set kindly provided by https://github.com/julianzaugg at #214

    library(tidyverse)
    library(ggforce)
    
    test_dataset <- tibble(x     = c(-235, -220,  187, -100, -162,   8, 336, 159, 443, -77, -412), 
                           y     = c( -32, -147, -123,    6,  102, -14,  40,   6,  15, 115,   30),
                           group = c( "a",  "a",  "a",  "b",  "b", "b", "c", "c", "c", "d",  "d"))
    
    ggplot(test_dataset, aes(x = x, y = y, fill = group)) + 
      geom_mark_ellipse() +
      geom_point(shape = 21, size = 3, color = "black")

    Session info
    R: 4.5.2 (2025-10-31)
    Platform: OSX Tahoe 26.3
    ggforce: 0.5.0.9000 (github repository version https://github.com/thomasp85/ggforce)
    ggplot2: 4.0.2

  2. leoduox commented on Feb 25, 2026

    @leoduox

    I also encountered the same problem.
    Image

  3. gruensch commented on Feb 25, 2026

    @gruensch

    not sure if it helps... cursor built a functional (at least for my so far run plots) version of the "mark_ellipse.R" script found in the https://github.com/thomasp85/ggforce/tree/main/R.

    Please test for proper functionality. My tests were not exhaustive and I am not fully familiar with ellipse functions.

    Could not upload the R extension file so I used a txt extension. You will find it here: mark_ellipse.txt

    If you want to test you can -> clone the https://github.com/thomasp85/ggforce repository in your computer -> replace the mark_ellipse.R with the linked mark_ellipse.txt -> change extension from txt to R -> install the local repo in R with

    devtools::install("my/local/ggforce_folder")
    -> test with the following set kindly provided by https://github.com/julianzaugg at #214

    library(tidyverse)
    library(ggforce)

    test_dataset <- tibble(x = c(-235, -220, 187, -100, -162, 8, 336, 159, 443, -77, -412),
    y = c( -32, -147, -123, 6, 102, -14, 40, 6, 15, 115, 30),
    group = c( "a", "a", "a", "b", "b", "b", "c", "c", "c", "d", "d"))

    ggplot(test_dataset, aes(x = x, y = y, fill = group)) +
    geom_mark_ellipse() +
    geom_point(shape = 21, size = 3, color = "black")

    Session info
    R: 4.5.2 (2025-10-31)
    Platform: OSX Tahoe 26.3
    ggforce: 0.5.0.9000 (github repository version https://github.com/thomasp85/ggforce)
    ggplot2: 4.0.2

    So far, this fix works (just ran it over a couple of cases where geom_mark_ellipse failed to set the correct angle before).
    thank you!

  4. maartenciers commented on Jul 8, 2026

    @maartenciers

    Any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions