ggforce icon indicating copy to clipboard operation
ggforce copied to clipboard

Unnecessary offset in place_labels?

Open assaron opened this issue 9 months ago • 1 comments

Hi,

I was trying to figure out, why I can't get more compact labeling in https://github.com/thomasp85/ggforce/issues/333, and noticed that there seem to be an unnecessary offset in place_labels function (https://github.com/thomasp85/ggforce/blob/main/R/mark_label.R#L70):

polygons[[length(polygons) + 1]] <- polyoffset(rect, 10)

As far as I understand, it creates and additional space around the label. However, rects already accounts for label.margin, so I don't understand what's the point of an additional offset. Changing the offset from 10 to 0 greatly improved the label positioning for me.

-- Alexey

assaron avatar Mar 21 '25 22:03 assaron

To highlight the idea, here are the two versions, from the master branch (first image) and with zero offset (second image). Notice, how "DC" and "Platelet" annotations move far away from their region in the first version.

Image

Image

I use the following code:

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

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

ggplot(data, aes(x=UMAP_1, y=UMAP_2)) +
    geom_point(aes(color=GNLY), size=0.5) +
    scale_color_gradient2(low = "#404040", high="red") +
    new_scale_color() +
    geom_shape(data=maskTable, aes(group=group, color=cluster),
               linewidth=1, fill=NA, expand=unit(-1, "pt"), show.legend = FALSE) +
    geom_mark_hull(data=maskTable, fill = NA, aes(group=cluster, color=cluster, label = cluster),
                   linewidth=0,
                   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,
                   con.colour = "inherit",
                   show.legend = FALSE) +
    # expanding to give a bit more space for labels
    scale_x_continuous(expand = ggplot2::expansion(mult = 0.1)) +
    scale_y_continuous(expand = ggplot2::expansion(mult = 0.1)) +
    coord_fixed() +
    theme_classic()

assaron avatar Mar 24 '25 15:03 assaron