YuLab-SMU / scatterpie

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

supporting add label for each slice of pie #49

Closed xiangpin closed 3 months ago

xiangpin commented 3 months ago
> library(ggplot2)
> library(scatterpie)
> d <- data.frame(x=rnorm(5), y=rnorm(5))
> d$A <- abs(rnorm(5, sd=1))
> d$B <- abs(rnorm(5, sd=2))
> d$C <- abs(rnorm(5, sd=3))
> ggplot() +
  geom_scatterpie(
    aes(x=x, y=y), data = d, cols=c("A", "B", "C"),
    label_radius=1.05
  ) +
  coord_fixed()

image

> d <- tidyr::gather(d, key="letters", value="value", -x:-y)
> d |> dplyr::select(c(x, y)) |> dplyr::distinct() |> dplyr::mutate(Cell=c('A','A','B','C','B')) -> d2
> d |> dplyr::left_join(d2) -> d3
Joining with `by = join_by(x, y)`
> d3$r_size <- c(2, 3, 4, 5, 6) * .01
> p4 <- ggplot() +
        geom_scatterpie(data = d3,
                        mapping = aes(x, y = y, r = r_size),
                        cols = 'letters',
                        long_format = TRUE,
                        label_radius = 1.1,
                        label_show_ratio = FALSE,
                        label_threshold = 0.06,
                        fontsize = 3
        ) +
        coord_fixed()
> p4

image