YuLab-SMU / scatterpie

:art: scatter pie plot
https://cran.r-project.org/package=scatterpie
60 stars 16 forks source link

Add the option for breaks in geom_scatterpie_legend #39

Closed tplehto closed 1 year ago

tplehto commented 1 year ago

Hello,

Thank you for the package! I have found it to be very useful.

I am developing a plotting pipeline, which uses scatterpie as a dependency. I found, that the automatically determined breaks for the radius in geom_scatterpie_legend are often suboptimal, especially in small number ranges. Here, the breaks often become decimal numbers, even when integers might be desired.

Someone raised this issue in stack overflow, and another person already provided possibly ready-to-ship code with the option to customize breaks (pasted below). I have found this tweak incredibly useful. https://stackoverflow.com/a/75719059

Would it be possible to implement this officially in a future version? The only caveat, that custom breaks brings with the current visualization of the scales, is that the text labels might start to overlap, if the breaks are too close to each other.

Thank you for your time and consideration.

Best Timo-Pekka Lehto

`geom_scatterpie_legend <- function (radius = NULL, breaks = NULL, x, y, n = 5, labeller) { if (n <= 1) { stop("The n argument requires larger than 1.") } if (is.null(breaks)) { radius <- (scales::breaks_extended(n = n))(radius)
} else { radius <- breaks }

label <- FALSE if (!missing(labeller)) { if (!inherits(labeller, "function")) { stop("labeller should be a function for converting radius") } label <- TRUE } dd <- data.frame(r = radius, start = 0, end = 2 pi, x = x, y = y + radius - max(radius), maxr = max(radius)) if (label) { dd$label <- labeller(dd$r) } else { dd$label <- dd$r } list(geom_arcbar(aes(x0 = ~x, y0 = ~y, r0 = ~r, r = ~r, start = ~start, end = ~end), data = dd, inherit.aes = FALSE), geomsegment(aes(x = ~x, xend = ~x + maxr 1.5, y = ~y + r, yend = ~y + r), data = dd, inherit.aes = FALSE), geomtext(aes(x = ~x + maxr * 1.6, y = ~y + r, label = ~label), data = dd, hjust = "left", inherit.aes = FALSE)) }`

GuangchuangYu commented 1 year ago

will be available in version = 0.2.1