How to plot a pie chart onto a map

2,095 views
Skip to first unread message

UkrBuckeye

unread,
Nov 29, 2011, 11:08:55 PM11/29/11
to ggplot2, alosh...@gmail.com
Dear gglot2 connoisseurs,

I am just a beginner here, so I apologize in advance for what may seem
as an easy question to you.

I am trying to plot/superimpose/overlay a pie chart onto the map of
Ukraine. I was able to produce a map and plot a dot plot on it. I was
also able to plot a trial pie chart separately from the map. Now, how
do I combine the two?

Your help would be greatly appreciated,

Yuliia

Please find my code below:

mappp<-ggplot()
mappp <- mappp + geom_polygon( data=mapwregions, aes(x=long, y=lat,
group = group),colour="black", fill="grey60" )
mappp #builds a map on a lang/long plot
mydata <- read.csv("trial.csv", header=TRUE, row.names=1, sep=",") #I
am uploading my data file
mappp <- mappp + geom_point(data=mydata, aes(x=long, y=lat,
size=number_of_respokanje <- c(37, 101, 200)), color="red1")
+scale_area(to = c(20, 35))
mappp <- mappp + geom_text( data=mydata, hjust=0.5, vjust=-0.5,
aes(x=long, y=lat, label=label), colour="black", size=4 )
mappp

p=ggplot(data=zz, aes(x="", y="", fill=Resp_Distr)) +
geom_bar(width=1) + coord_polar("y")
pie <- ggplot(data, aes(x = factor(1), fill = cluster, weight=size)) +
geom_bar(width = 1) + coord_polar(theta="y")

UkrBuckeye

unread,
Nov 30, 2011, 10:06:57 AM11/30/11
to ggplot2
Anyone? Please? :(

Brandon Hurr

unread,
Nov 30, 2011, 10:34:51 AM11/30/11
to UkrBuckeye, ggplot2
Could you supply your data sets?

dput(mapwregions)
dput(mydata)
dput(zz)

At first glance I would say that this is probably not easily doable since you're using coord_map for the map and then coord_polar for the pie chart. I don't think having two coordinate systems is possible unless you build it in two layers. I'm not very good at that stuff so you might have to wait for someone else to respond to that... 

Brandon

Anyone? Please? :(
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

UkrBuckeye

unread,
Nov 30, 2011, 11:47:09 AM11/30/11
to ggplot2
Thank you for the response.

I used just some random numbers, in order to see if it will work at
all and to understand the syntax/logic of the script.

I also think I also gave the wrong code for the pie chart. It should
be:

data <- data.frame(Response = paste(c("akanje","okanje","inbetween")),
size =c(60, 24, 16))
dpie <- ggplot(data, aes(x = factor(1), fill = Response, weight=size))


+ geom_bar(width = 1) + coord_polar(theta="y")

myplot = dpie + opts(title ="Responses Distribution")
myplot

David Kahle

unread,
Nov 30, 2011, 11:40:43 AM11/30/11
to Brandon Hurr, UkrBuckeye, ggplot2
Here's a simple proof of concept (using the new package ggmap) -

library(ggmap)
ukraine <- qmap(location = 'ukraine', fullpage = TRUE)

n <- 10
fake_data <- factor(gl(3, n, label = c('a','b','c')))[sample(3*n,.50*3*n)]
f_df <- data.frame(x = factor(1), y = fake_data)
pie <- qplot(x, y, data = f_df, geom = 'bar', fill = y) + 
  coord_polar(theta = 'y') + theme_nothing()

    

    

vplayout <- function(x, y)  viewport(layout.pos.row = x, layout.pos.col = y)     
grid.newpage()
pushViewport(viewport(layout = grid.layout(19,19))) 
print(ukraine, vp = vplayout(1:19, 1:19))
print(pie, vp = vplayout(10, 10))

The bottom part is how you simply push to the viewports, if that is what you're looking to do....  The plot that you are trying to do with the polygon stuff is done the same way.

Cheers
david.

David Kahle

unread,
Nov 30, 2011, 11:56:58 AM11/30/11
to Brandon Hurr, UkrBuckeye, ggplot2
Here's a simple proof of concept (using the new package ggmap) -

library(ggmap)
ukraine <- qmap(location = 'ukraine', fullpage = TRUE)

n <- 10
fake_data <- factor(gl(3, n, label = c('a','b','c')))[sample(3*n,.50*3*n)]
f_df <- data.frame(x = factor(1), y = fake_data)
pie <- qplot(x, y, data = f_df, geom = 'bar', fill = y) + 
  coord_polar(theta = 'y') + theme_nothing()

    

    

vplayout <- function(x, y)  viewport(layout.pos.row = x, layout.pos.col = y)     
grid.newpage()
pushViewport(viewport(layout = grid.layout(19,19))) 
print(ukraine, vp = vplayout(1:19, 1:19))
print(pie, vp = vplayout(10, 10))

The bottom part is how you simply push to the viewports, if that is what you're looking to do....  The plot that you are trying to do with the polygon stuff is done the same way.

Cheers
david.



On Nov 30, 2011, at 10:34 AM, Brandon Hurr wrote:

Yuliia Aloshycheva

unread,
Nov 30, 2011, 12:07:43 PM11/30/11
to David Kahle, Brandon Hurr, ggplot2
Thank you!

However, when trying to download the package I got this message:

"package ‘ggmap’ is not available (for R version 2.14.0)"


2011/11/30 David Kahle <david...@gmail.com>



--
Sincerely,

            Yuliia

pie on map.png

David Kahle

unread,
Nov 30, 2011, 12:12:18 PM11/30/11
to Yuliia Aloshycheva, Brandon Hurr, ggplot2
It's newly uploaded (as of this morning, actually), so not all of the binaries are available.  Give it a couple days.  Until then, you can install from the source on cran.

HTH
david.

On Nov 30, 2011, at 11:07 AM, Yuliia Aloshycheva wrote:

Thank you!

However, when trying to download the package I got this message:

"package ‘ggmap’ is not available (for R version 2.14.0)"


2011/11/30 David Kahle <david...@gmail.com>
Here's a simple proof of concept (using the new package ggmap) -

library(ggmap)
ukraine <- qmap(location = 'ukraine', fullpage = TRUE)

n <- 10
fake_data <- factor(gl(3, n, label = c('a','b','c')))[sample(3*n,.50*3*n)]
f_df <- data.frame(x = factor(1), y = fake_data)
pie <- qplot(x, y, data = f_df, geom = 'bar', fill = y) + 
  coord_polar(theta = 'y') + theme_nothing()
    
    
vplayout <- function(x, y)  viewport(layout.pos.row = x, layout.pos.col = y)     
grid.newpage()
pushViewport(viewport(layout = grid.layout(19,19))) 
print(ukraine, vp = vplayout(1:19, 1:19))
print(pie, vp = vplayout(10, 10))

The bottom part is how you simply push to the viewports, if that is what you're looking to do....  The plot that you are trying to do with the polygon stuff is done the same way.

Cheers
david.

<pie on map.png>





--
Sincerely,

            Yuliia


Bret Shandro

unread,
Mar 31, 2016, 8:20:28 PM3/31/16
to ggplot2, alosh...@gmail.com, brando...@gmail.com
Hi, I know this this is an old thread but I thought I would try...

I get the following error when I try this proof:

Brandon Hurr

unread,
Apr 1, 2016, 10:26:26 AM4/1/16
to Bret Shandro, ggplot2, Yuliia Aloshycheva
You need to load the grid package too. 

library(grid)


Brandon Hurr

unread,
Apr 1, 2016, 2:11:15 PM4/1/16
to Bret Shandro, ggplot2
Bret, 

It appears that some other changes in ggplot2 are biting us. The change I made was to summarize the counts before plotting and following the pacman example in ?coord_polar

HTH, 
B

library(ggplot2)
library(dplyr)
library(ggmap)

ukraine <- qmap(location = 'ukraine', fullpage = TRUE)

n <- 10
fake_data <- factor(gl(3, n, label = c('a','b','c')))[sample(3*n,.50*3*n)]
f_df <- data.frame(x = factor(1), y = fake_data) %>%
group_by(y) %>%
summarise(count = n())

pie <- ggplot(data = f_df, aes(x="", y = count, fill = y)) +
geom_bar(width = 1, stat="identity") + 
coord_polar(theta = 'y') +
theme_nothing()
    
vplayout <- function(x, y)  viewport(layout.pos.row = x, layout.pos.col = y)     
grid.newpage()
pushViewport(viewport(layout = grid.layout(19,19))) 
print(ukraine, vp = vplayout(1:19, 1:19))
print(pie, vp = vplayout(10, 10))


Inline image 1


On Fri, Apr 1, 2016 at 10:12 AM, Bret Shandro <bsha...@gmail.com> wrote:
Thanks, that should have been obvious! I have copied the proof exactly however, now the error is:

Error: stat_count() must not be used with a y aesthetic.

Brandon Hurr

unread,
Apr 1, 2016, 2:13:25 PM4/1/16
to Bret Shandro, ggplot2
You need grid too, sorry.


library(ggplot2)
library(dplyr)
library(ggmap)
library(grid) 
Reply all
Reply to author
Forward
0 new messages