2

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.

GabrielMontenegro
  • 683
  • 1
  • 6
  • 21

2 Answers2

2

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

Adilson V Casula
  • 164
  • 1
  • 16
0

you can create a pie-chart with ggplot2 and use grid.arrange

library(gridExtra)
library(ggplot2)

#creating the pie.chart (random data)
df <- data.frame(
  group = c("Male", "Female", "Child"),
  value = c(25, 25, 50)
  )

bp <- ggplot(df, aes(x="", y=value, fill=group)) +
geom_bar(width = 1, stat = "identity")

pie <- bp + coord_polar("y", start=0)

then, creating the ssplot

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

finally, the both plots

grid.arrange(pie, plot2, nrow=2)

enter image description here

Adilson V Casula
  • 164
  • 1
  • 16