rggplot2scatterpie

geom_scatterpie with a legend that is unscaled


I would like to have the radius of my piecharts scaled by sample size (n) on a map. However, the variance in n is large, so I would like rescale it on the map, say from 0 to 1 (this could be a log, or anything really). I would like the legend to show the scaled sizes, but give the actual values of n, not the scales.

So, in the example below, the legend would show circles of widths 0 to 1, but with each tick mark on the circles labelled 0 through 500. Thus the viewer would understand that the circles are scaled, but could get a sense of the actual sample size, not just the relative, scaled sample size.

For example:

library(ggplot)
library(scatterpie)

df <- data.frame(id = c("a", "b", "c"), 
                 lat = c(30, 31, 32), 
                 long = c(-85, -86, -87),
                 a = c(1, 3, 5),
                 b = c(8, 7, 4),
                 c = c(1, 0, 1),
                 n = c(1, 200, 500))

# Plotting without scaling yields pies that will not fit on a map (the axes are too large)
ggplot()+
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = n),
                      cols = c("a", "b", "c"))+
  geom_scatterpie_legend(df$n, x = -85.5, y = 32.5)

# Plotting the rescaled size is also a bit big, but importantly shows the scaled values in the legend

# Rescale n to 0 to 1
df$r <- scales::rescale(df$n, to = c(0, 1))

ggplot()+
  geom_scatterpie(data = df,
                  aes(y = lat, x = long,
                      group = id,
                      r = r),
                  cols = c("a", "b", "c"))+
  geom_scatterpie_legend(df$r, x = -85.5, y = 32.5)

Is there a better way to rescale in geom_scatterpie so 1) scales with large variance allow all pies to be visible, and 2) the legend shows the actual values, not the scaled values?


Solution

  • You can manually rescale by simply dividing the count by the maximum count inside aes. This is reversed inside geom_scatterpie_legend using the labeller argument:

    ggplot()+
      geom_scatterpie(data = df,
                      aes(y = lat, x = long,
                          group = id,
                          r = n / max(n)),
                      cols = c("a", "b", "c")) +
      geom_scatterpie_legend(df$n/max(df$n), labeller = function(x) x * max(df$n),
                             x = -84.5, y = 31) +
      coord_equal()
    

    enter image description here