2017-08-09 162 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