YuLab-SMU / scatterpie

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

Allow label customisation in scatterpie legend #38

Closed mattiaghilardi closed 1 year ago

mattiaghilardi commented 1 year ago

In one of my projects I wanted to display the scatterpie legend labels on the left side of the legend key. This can be done in ggplot2 legends, but I realised that it was not possible to do this for the scatterpie legend. I added the label_position argument to geom_scatterpie_legend() to allow this behaviour. I also added the ... argument which allows you to customise the key labels (colour, size, font, ...). This PR introduces these arguments as I think users could benefit from greater flexibility.

Reprex (using example from the vignette):

# devtools::install_github("mattiaghilardi/scatterpie", ref = "scatterpie-legend-custom-text")
library(scatterpie)
set.seed(123)
long <- rnorm(50, sd=100)
lat <- rnorm(50, sd=50)
d <- data.frame(long=long, lat=lat)
d <- with(d, d[abs(long) < 150 & abs(lat) < 70,])
n <- nrow(d)
d$region <- factor(1:n)
d$A <- abs(rnorm(n, sd=1))
d$B <- abs(rnorm(n, sd=2))
d$C <- abs(rnorm(n, sd=3))
d$D <- abs(rnorm(n, sd=4))
d[1, 4:7] <- d[1, 4:7] * 3

d$radius <- 6 * abs(rnorm(n))
p <- ggplot() +
  geom_scatterpie(aes(x=long, y=lat, group=region, r=radius),
                  data=d,
                  cols=LETTERS[1:4], color=NA) +
  coord_equal()
p + geom_scatterpie_legend(d$radius, x=-140, y=-70)

# legend at bottom-right with labels at left
p + geom_scatterpie_legend(d$radius, x=140, y=-70, label_position="left")

# example of label customisation: smaller text, in bold and serif font
p + geom_scatterpie_legend(d$radius, x=-140, y=-70, fontface="bold", size=3.5, family="serif")

GuangchuangYu commented 1 year ago

@xiangpin can you review this PR?

GuangchuangYu commented 1 year ago

thanks