Search code examples
rplotpie-chartr-sp

Plot a pie-chart on top of a spplot in R


I want to plot a pie-chart on top of a gridded data that I have managed to plot.

The data:

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

Following this post: R plot grid value on maps, I used spplot to plot the data using:

gridded(nasa) <- c("Lon","Lat")
spplot(nasa, "Ann")

I have been trying several functions to plot pie-charts on top of the plot, but haven't managed to do it:

If I use the floating.pie function after plotting the map, I get an error.

floating.pie(5,2, c(3,2,5,1), radius=1)  
Error in polygon(xc, yc, col = col[i], ...) : 
  plot.new has not been called yet   

Using par(new=TRUE) after drawing an empty plot, makes me able to draw a pie-chart,but the coordinate are based on the new plot.

Is there a way to plot a pie-chart on top of a spplot?

I checked pieSP, but couldn't manage to plot it either.


Solution

  • I'll not delete the other answer, because it can help someone.

    But we can try this one (ugly pie charts). In this option, you can't use ggplot2 (at least i don't know how to)

    library(lattice)
    library(gridBase)
    library(grid) 
    
    
    plot.new()
    pushViewport(viewport())
    plot1
    pushViewport(viewport(x=.5,y=.5,width=.15,height=.15))    #this will change position of pie
    #grid.rect()
    par(plt = gridPLT(), new=TRUE)
    pie(c(25, 25, 50))             # I've tried push "pie" like did with "plot1" but it doesn't work
    

    the grid.rect() will draw a box outer of your pie chart. I hope it works

    world with a pie