2017-08-09 217 views
1

我想在我設法繪製的網格數據頂部繪製餅圖。在R中繪製餅圖頂部的圖表

數據:

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

下面這個帖子:R plot grid value on maps,我用spplot繪製數據使用:

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

我一直在嘗試多種功能之上繪製餅圖情節,但沒有設法做到這一點:

如果我在繪製地圖後使用floating.pie函數,我得到一個錯誤。

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 

繪製一個空的陰謀後使用par(new=TRUE),讓我能畫一個餅形圖,而是基於新的圖形座標。

有沒有辦法在spplot的頂部繪製餅圖?

我檢查了pieSP,但無法設法繪製它。

回答

1

我不會刪除其他答案,因爲它可以幫助某人。

但我們可以試試這個(醜陋的餅圖)。 在這個選項中,你不能使用GGPLOT2(至少我不知道如何)

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 

grid.rect()將吸引你的餅圖的箱外。 我希望它的工作原理

world with a pie

0

您可以創建一個餅圖與GGPLOT2和使用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) 

然後,創建ssplot

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

最後,兩條曲線

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

enter image description here

+0

感謝。我的意思是位於地圖頂部。說非洲之巔。 – GabrielMontenegro