0

I want to plot pie charts using geom_scatterpie on top of a geom_tile plot. However, I am getting an error:

Error: Discrete value supplied to continuous scale

Here's the simple code that I cannot get to work:

library(ggplot2)
library(scatterpie)

nasafile <- "http://eosweb.larc.nasa.gov/sse/global/text/global_radiation"
nasa <- read.table(file=nasafile, skip=13, header=TRUE)

p <- ggplot(aes(y = Lat , x = Lon), data = nasa )+
      geom_tile(aes(fill=Ann)) +
      scale_fill_gradientn(colours=brewer.pal('YlOrRd', n=9)) +
      theme_bw() +
      coord_equal()
plot(p)

This works, but if I add the geom_scatterpie on top of that:

First the data for the pie charts to plot:

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

But I get the error when I do this:

p + geom_scatterpie(aes(x=x, y=y), data=d, cols=c("A", "B", "C")) + coord_fixed()
GabrielMontenegro
  • 683
  • 1
  • 6
  • 21

1 Answers1

3

The problem is that your geom_tile uses a continuous fill scale while geom_scatterpie uses a discrete fill scale. It works if you change Ann to a factor. Not ideal, but this works:

nasa$Ann <- as.factor(as.integer(nasa$Ann))
mypalette <- brewer.pal(9, "YlOrRd") # 6 for geom_tile, 3 for geom_scatterpie
p <- ggplot(aes(y = Lat , x = Lon), data = nasa )+
    geom_tile(aes(fill=Ann)) +
    scale_fill_manual(values = mypalette) +
    theme_bw() +
    coord_equal()
p

d <- data.frame(x=rnorm(5, 0, 50), y=rnorm(5, 0, 30))    # larger sd
d$A <- abs(rnorm(5, sd=1))
d$B <- abs(rnorm(5, sd=2))
d$C <- abs(rnorm(5, sd=3))

p + geom_scatterpie(aes(x=x, y=y, r = 20), data=d, cols=c("A", "B", "C"))  #larger radius

Or, using, size= instead of fill= (and no geom_scatterpie):

p <- ggplot(aes(y = Lat , x = Lon), data = nasa )+
    geom_tile(aes(fill=Ann)) +
    scale_fill_gradientn(colours=brewer.pal('YlOrRd', n=9)) +
    theme_bw() +
    coord_equal()
p

d <- data.frame(Lon = c(-100, 0, 100),
                Lat = c(-50, 0, 50),
                genvar = c(.1, .3, .5))

p + geom_point(data = d, aes(x = Lon, y = Lat, size = genvar),
               color = "white")
jtr13
  • 1,225
  • 11
  • 25
  • Thanks for the answer. The plot lost its resolution, but I think it answers the question on how to use geom_scatterpie here. If you know a better way to plot this map, including the pie charts, would you mind posting that question? Thanks again. – GabrielMontenegro Aug 04 '17 at 13:21
  • Honestly, I like the high res version of your map and don't like the scatterpies. What is the nature of the data you want to add? – jtr13 Aug 04 '17 at 13:30
  • It is the frequency of a genetic variant in different populations from the world. I want to reproduce this figure: http://journals.plos.org/plosgenetics/article?id=10.1371/journal.pgen.0040032 – GabrielMontenegro Aug 04 '17 at 14:25
  • The trick is to use some other scale, not fill. Since there are only two categories in the pie charts, could you express genetic variation as a percentage of the total, in order to reduce the variable to a single number? See edits above. – jtr13 Aug 04 '17 at 14:59
  • Thank you. But if I wanted to plot them using pie charts? I was trying to use the coord_polar() function to plot them on top of my map, but failed. There is a post here, but the data is not available anymore and therefore I wasn't able to reproduce the example: https://stackoverflow.com/questions/10368180/plotting-pie-graphs-on-map-in-ggplot – GabrielMontenegro Aug 04 '17 at 15:17
  • 1
    AFAIK you can't use coord_polar for multiple pie charts. I don't know of a way other than what I suggested with lower res (doesn't mean of course that it's not possible...) – jtr13 Aug 04 '17 at 15:24