pie chart + world map for Genetics

**SCATTER PIE **
Hey guys!
I was able to plot some data into a world map. Specifically some pie charts for different countries in the world, about some rsID (genetics).
The problem I have is about creating a legend for the different pie chart sizes, in order to make it more clear about the sample size regarding the pie chart size [IMAGE 1]. Thas has been succesfully done. BUT the problem I have is that the labels for such 'legends' are to big (pie chart legend font size bigger than other elements in the graphic).

###############################################################################

These are the lines of code regarding such problem:

worldmap<- map_data ("world")
mapplot1<- ggplot(worldmap) +

geom_map(data = worldmap, map = worldmap, aes(x = long, y = lat, map_id = region), fill = "gray70") +

geom_scatterpie(aes(x = x_coord, y = y_coord, r=radius), data = table_pop, cols = c("MAF", "MajAF")) +

coord_fixed() + 

 geom_scatterpie_legend((ref$radius), x = -180, y = -50, n=7, labeller= function(a) a = (ref$label))

##########################################################################

So I was wondering if you guys had any idea on what to do or change.
Please consider that 'theme()' only works on the legend below said graph.
So maybe change '(ref$radius)' fontsize??? Or any other thing that I missed.


Thanks in advance.

1 Like

I overrode the function to customise it.

example data

#example data
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

code example

library(scatterpie)
custom_geom_scatterpie_legend <- 
  function (radius, x, y, n = 5, labeller,textsize=1) 
  {
    if (length(radius) > n) {
      radius <- unique(sapply(seq(min(radius), max(radius), 
                                  length.out = n), scatterpie:::round_digit))
    }
    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(ggforce:::geom_arc_bar(aes_(x0 = ~x, y0 = ~y, r0 = ~r, r = ~r, 
                           start = ~start, end = ~end), data = dd, inherit.aes = FALSE), 
         geom_segment(aes_(x = ~x, xend = ~x + maxr * 1.5, y = ~y + 
                             r, yend = ~y + r), data = dd, inherit.aes = FALSE), 
         geom_text(aes_(x = ~x + maxr * 1.6, y = ~y + r, label = ~label), 
                   data = dd, hjust = "left", inherit.aes = FALSE,size=textsize))
  }


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()

#different sizes 
p + custom_geom_scatterpie_legend(d$radius, x=-120, y=-70,textsize = 3)
# different
p + custom_geom_scatterpie_legend(d$radius, x=-120, y=-70,textsize = 2)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.