Wednesday, April 24, 2013

RG#88: Plot pie over google map

require(ggplot2)
library(ggmap)
require(grid)

 # blank theme function
blk_theme <- function (){theme(
     line = element_blank(),
     rect = element_blank(),
     text = element_blank(),
     axis.ticks.length = unit(0, "cm"),
     axis.ticks.margin = unit(0.01, "cm"),
     legend.position = "none",
     panel.margin = unit(0, "lines"),
     plot.margin = unit(c(0, 0, -.5, -.5), "lines"),
     complete = TRUE
   )
   }
  
dha = get_map(location = c(lon = 80.56410278, lat = 28.7089375), zoom = 14, maptype = 'roadmap', source = "google")
dhamp <-  ggmap(dha) + blk_theme ()


# data 1
d1 <- sample ( c("A", "B", "C", "D"), 100, replace = TRUE)
d2 <- sample ( c("A", "B", "C"), 100, replace = TRUE)
d3 <- sample ( c("A", "B"), 100, replace = TRUE)
d4 <- sample ( c("A", "B"), 100, replace = TRUE)
d5 <- sample ( c("A", "B", "C", "D"), 100, replace = TRUE)

myd <- data.frame(x = factor(1),d1, d2, d3, d4, d5)

# pie charts
pie1 <- qplot(x, d1, data = myd, geom = 'bar', fill = d1) +
  coord_polar(theta = 'y') + blk_theme()
pie2 <- qplot(x, d2, data = myd, geom = 'bar', fill = d2) +
  coord_polar(theta = 'y') + blk_theme()
pie3 <- qplot(x, d3, data = myd, geom = 'bar', fill = d3) +
  coord_polar(theta = 'y') + blk_theme()
pie4 <- qplot(x, d4, data = myd, geom = 'bar', fill = d4) +
  coord_polar(theta = 'y') + blk_theme()
pie5 <- qplot(x, d5, data = myd, geom = 'bar', fill = d5) +
  coord_polar(theta = 'y') + blk_theme()

# plotting and viewports
# function
vplayout <- function(x, y)viewport(layout.pos.row = x, layout.pos.col = y)
####
grid.newpage()
pushViewport(viewport(layout = grid.layout(20,20)))
# print mainmap
print(dhamp, vp = vplayout(1:20, 1:20))

  # need to find manually, maximum and minimum lon and lat
 # click on google earth to find
 maximum.lon <- 80.591
 minimum.lon <- 80.538
 maximum.lat <- 28.735
 minimum.lat <- 28.685

 # X and Y cordinates where pie need to draw
 pieposX <- c(28.72, 28.73, 28.70,28.73,28.69 )
 pieposY <- c(80.55, 80.58, 80.58, 80.55,80.58)

ycoord <- 20 * ((pieposX - minimum.lat)/(maximum.lat - minimum.lat))
xcoord <- 20 * ((pieposY - minimum.lon)/(maximum.lon - minimum.lon))

# plot pie over layer
print(pie1, vp = vplayout(xcoord[1], ycoord[1]))
print(pie2, vp = vplayout(xcoord[2], ycoord[2]))
print(pie3, vp = vplayout(xcoord[3], ycoord[3]))
print(pie4, vp = vplayout(xcoord[4], ycoord[4]))
print(pie5, vp = vplayout(xcoord[5], ycoord[5]))



# bigger pie plot  

grid.newpage()
pushViewport(viewport(layout = grid.layout(7,7)))
# print mainmap
print(dhamp, vp = vplayout(1:7, 1:7))

  # need to find manually, maximum and minimum lon and lat
 # click on google earth to find
 maximum.lon <- 80.591
 minimum.lon <- 80.538
 maximum.lat <- 28.735
 minimum.lat <- 28.685

 # X and Y cordinates where pie need to draw
 pieposX <- c(28.72, 28.73, 28.70,28.73,28.69 )
 pieposY <- c(80.55, 80.58, 80.58, 80.55,80.58)

ycoord <- 7 * ((pieposX - minimum.lat)/(maximum.lat - minimum.lat))
xcoord <- 7 * ((pieposY - minimum.lon)/(maximum.lon - minimum.lon))

# plot pie over layer 
print(pie1, vp = vplayout(xcoord[1], ycoord[1]))
print(pie2, vp = vplayout(xcoord[2], ycoord[2]))
print(pie3, vp = vplayout(xcoord[3], ycoord[3]))
print(pie4, vp = vplayout(xcoord[4], ycoord[4]))
print(pie5, vp = vplayout(xcoord[5], round (ycoord[5],0)))







No comments:

Post a Comment

Note: Only a member of this blog may post a comment.