Showing posts with label bubble plot. Show all posts
Showing posts with label bubble plot. Show all posts

Thursday, April 18, 2013

RG#79: Heatmap with overlayed circle (size and color)


set.seed (78888)
rectheat = sample(c(rnorm (10, 5,1), NA, NA), 150, replace = T)
circlefill =  rectheat*10 + rnorm (length (rectheat), 0, 3)
circlesize = rectheat*1.5 + rnorm (length (rectheat), 0, 3)
myd <- data.frame (rowv = rep (1:10, 15), columnv = rep(1:15, each = 10),
          rectheat, circlesize, circlefill)
          
          
require(ggplot2)
 pl1 <-  ggplot(myd, aes(y = factor(rowv),  x = factor(columnv))) +  geom_tile(aes(fill = rectheat)) +  scale_fill_continuous(low = "blue", high = "green")


  pl1  +      geom_point(aes(colour = circlefill,  size =circlesize))  +    scale_color_gradient(low = "yellow",   high = "red")+     scale_size(range = c(1, 20))+   theme_bw()


Monday, April 15, 2013

RG#65: Get google map and plot data in it


# get map 
library(ggmap)
# example of map of Dhangadhi, Nepal  
dhanmap1 = get_map(location = c(lon = 80.56410278, lat = 28.7089375), zoom = 12, maptype = 'roadmap', source = "google")
dhanmap1 = ggmap(dhanmap1)
dhanmap1 

#zoomed map: 

# example of map of Dhangadhi, Nepal  
dhanmap2 = get_map(location = c(lon = 80.56410278, lat = 28.7089375), zoom = 14, maptype = 'roadmap', source = "google")
dhanmap2 = ggmap(dhanmap2)
dhanmap2



#zoomed map and satellite type 

# example of map of Dhangadhi, Nepal  
dhanmap3 = get_map(location = c(lon = 80.56410278, lat = 28.7089375), zoom = 18, maptype = "satellite", source = "google")
dhanmap3 = ggmap(dhanmap3)
dhanmap3

# plotting over map 

dhanmap5 = get_map(location = c(lon = 80.56410278, lat = 28.7089375), zoom = 14, maptype = 'roadmap', source = "google")
dhanmap5 = ggmap(dhanmap5)

# data
set.seed(1234)
lon <- runif (40, 80.54, 80.59)
lat <- runif (40, 28.69, 28.73) 
varA = rnorm (40, 20, 10)
myd <- data.frame (lon, lat, varA)


# the bubble chart
library(grid)
dhanmap5 +   geom_point(aes(x = lon, y = lat, colour = varA, size = varA, alpha = 0.9), data = myd)  + scale_colour_gradient(low="yellow", high="red")




# data
set.seed(1234)
lon <- runif(50, 80.54, 80.59)
lat <- runif(50, 28.69, 28.73 )
varA = sample (c(1:5), 50, replace = TRUE)
myd <- data.frame (lon, lat, varA)
dhanmap5  + stat_bin2d(aes(x = lon, y = lat, colour = varA, fill = factor(varA)), 
size = .10, alpha = 0.5, data = myd)
 





# get map with name
library(ggmap)
# example of map of Chicago  
chicagomap = get_map(location = "Chicago", zoom = 13, maptype = "hybrid" , source = "google")
chicagomap = ggmap(chicagomap)
chicagomap






Sunday, April 14, 2013

RG#62: spartial buble plot


require(sp)

# data

data(meuse)
coordinates(meuse) <- c("x", "y") # promote to SpatialPointsDataFrame

bubble(meuse, "zinc", maxsize = 2.5, main = "zic concentrations (ppm)", key.entries = 2^(-1:4))

 

RG#61: Plotting US or World Cities

require(maps)
map("world")
data(world.cities)
# cities with minimum 20,000 population
map.cities(world.cities, country = "", minpop = 20000, maxpop = Inf,
pch = ".", col = "red")





map("world", col = "gray70", fill = TRUE)
data(world.cities)

# now device colors from yvar data category
# brewing color for continious color filling



 library(RColorBrewer)
plotclr <- brewer.pal(7,"YlOrRd")#



# categorize in different class for yvar
world.cities$colrs <- as.numeric(cut(world.cities$pop, c(0, 250000, 500000, 750000, 1000000, 1250000, 1500000,Inf)))

# corresponding legend text
legdtxt <- c("<0.25M", "0.25-0.50M", "0.50-0.75M", "0.75-1M", "1-1.5M", ">1.5M")

 map.cities(world.cities, country = "", minpop = 200000, maxpop = Inf, pch = 19, col = plotclr[world.cities$colrs])
legend("bottomleft", legdtxt, horiz = FALSE, fill = plotclr)
 


# world map cities pch proportional to population and color 
map("world", col = "lightgreen", fill = TRUE)
 
data (world.cities)
 
library(RColorBrewer)
plotclr <- brewer.pal(7,"YlOrRd")#
# categorize in different class for yvar world.cities$colrs <- as.numeric(cut(world.cities$pop, c(0, 250000, 500000, 750000, 1000000, 1250000, 1500000,Inf)))
# corresponding legend text
legdtxt <- c("<0.25M", "0.25-0.50M", "0.50-0.75M", "0.75-1M", "1-1.5M", ">1.5M")
 


map.cities(world.cities, country = "", minpop = 100000, maxpop = Inf, pch = 19,
 col =  plotclr[world.cities$colrs], cex = world.cities$colrs *0.5)
 
legend("bottomleft", legdtxt, horiz = FALSE, fill = plotclr)




 # same map in US
 map('usa', fill = TRUE, col = "lightgreen")
 data(us.cities)
  library(RColorBrewer)
  plotclr <- brewer.pal(6,"YlOrBr")## categorize in different class for yvar
  us.cities$colrs <- as.numeric(cut(us.cities$pop, c(0, 50000, 500000, 750000, 1000000, Inf)))
   # corresponding legend text
   legdtxt <- c("<0.05M", "0.05-0.50M", "0.50-0.75M", "0.75-1M",  ">1M")
   map.cities(us.cities, country = "", minpop = 50000, maxpop = Inf, pch = 19,
    col =  plotclr[us.cities$colrs], cex = us.cities$colrs)
 legend("bottomleft", legdtxt, horiz = FALSE, fill = plotclr, cex = 0.6)



 
 
 

Monday, April 8, 2013

RG#33: Bubble plot

# Bubble plot
# data
dataf <- data.frame (X = c(12, 18, 20, 6, 14, 15, 13, 5, 2, 18, 25), Y = c(6, 8, 12, 16, 4, 5, 3, 5, 12, 8, 15), Z = c(4, 2, 4, 3, 4, 8, 6, 2, 2, 8, 5))

# in base
radius = dataf$Z / max(dataf$Z)
symbols(dataf$X, dataf$Y, circles=radius, inches=0.10, fg="white", bg=" darkorchid4")


require(ggplot2)
qplot(X, Y, data= dataf, size= Z, col = "darkorchid")+ theme_bw()