8000 Feature request: geom_mark_polygon · Issue #333 · thomasp85/ggforce · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature request: geom_mark_polygon #333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
assaron opened this issue Mar 21, 2025 · 4 comments
Open

Feature request: geom_mark_polygon #333

assaron opened this issue Mar 21, 2025 · 4 comments

Comments

@assaron
Copy link
assaron commented Mar 21, 2025

Related to: #321

I have my own algorithm to calculate the border of points group, but would like to use your label location procedure. Do you think it'd be possible to extract something like geom_mark_polygon from the current code?

This is the plot I'm interested to add labels to:

library(ggplot2)
library(ggforce)
library(data.table)

load(gzfile("border_example.rda.gz"))

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) + 
    geom_point(color="grey") + 
    geom_shape(data=maskTable, aes(group=group, color=cluster),
               linewidth=1, fill=NA, expand=unit(-1, "pt")) +
    coord_fixed() + 
    theme_classic()

border_example.rda.gz

Image

Currently, the best I can do is something like this, relying on geom_mark_hull:

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) +
    geom_point(color="grey") +
    geom_shape(data=maskTable, aes(group=group, color=cluster),
               linewidth=1, fill=NA, expand=unit(-1, "pt")) +
    geom_mark_hull(data=maskTable, aes(fill = NA, group=cluster, label = cluster),
                   radius=0, expand=0, con.cap=0) +
    coord_fixed() +
    theme_classic()

Image

However, I don't need to actually build a hull, since I have already have a proper polygon, and I don't want to have an additional dependency.

There are also couple of other questions:

  1. Several label connectors (for example, NK, CD8 T, and others) lead to a split point between two clusters, resulting in a bit of ambiguity. Is there a way to fix it?
  2. Is there a way to remove fill? fill=NA doesn't seem to work as I would expect.
  3. Is there a way to decrease the length of the connectors? They seem to be longer that I'd like, and I couldn't find an option to control it.
@assaron
Copy link
Author
assaron commented Mar 21, 2025

Update: I've resolved the additional questions:

Image

library(ggplot2)
library(ggforce)
library(data.table)

load(gzfile("border_example.rda.gz"))
ggplot(data, aes(x=UMAP_1, y=UMAP_2)) +
    geom_point(color="grey") +
    geom_shape(data=maskTable, aes(group=group, color=cluster),
               linewidth=1, fill=NA, expand=unit(-1, "pt")) +
    geom_mark_hull(data=maskTable, fill = NA, color=NA, aes(group=cluster, label = cluster),
                   radius=0, expand=0, con.cap=0, con.type = "straight",
                   label.fontsize = 10, label.buffer = unit(0, "cm"),
                   label.fontface = "plain",
                   label.minwidth = 0,
                   label.margin = margin(2, 2, 2, 2, "pt"),
                   label.lineheight = 0) +
    # expanding to give a bit more space for labels
    scale_x_continuous(expand = expansion(mult = 0.1)) +
    scale_y_continuous(expand = expansion(mult = 0.1)) +
    coord_fixed() +
    theme_classic()

So only the main question remains: to do it without building the unnessary hulls.

@assaron
Copy link
Author
assaron commented Mar 24, 2025

However, I don't need to actually build a hull, since I have already have a proper polygon, and I don't want to have an additional dependency.

I realized, that concaveman is no longer a dependency (unfortunately, not yet in a CRAN version), but anyway constructing a hull is unnecessary here, and can result in contour mismatches.

I think I can provide a draft pull request, if the general idea sounds reasonable.

@ppaxisa
Copy link
ppaxisa commented Apr 11, 2025

Hi @assaron, I'm also interested in this border plotting of groups. Do you have your algorithm available somewhere? thanks

@assaron
Copy link
Author
assaron commented Apr 11, 2025

@ppaxisa the border algorithm is available in https://github.com/alserglab/mascarade package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0