Skip to content

Instantly share code, notes, and snippets.

View Ram-N's full-sized avatar

Ram Narasimhan Ram-N

View GitHub Profile
library("ggmap")
library(maptools)
library(maps)
visited <- c("SFO", "Chennai", "London", "Melbourne", "Johannesbury, SA")
ll.visited <- geocode(visited)
visit.x <- ll.visited$lon
visit.y <- ll.visited$lat
#> dput(visit.x)
#Using GGPLOT, plot the Base World Map
mp <- NULL
mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders
mp <- ggplot() + mapWorld
#Now Layer the cities on top
mp <- mp+ geom_point(aes(x=visit.x, y=visit.y) ,color="blue", size=3)
mp
#USING MAPS
map("world", fill=TRUE, col="white", bg="lightblue", ylim=c(-60, 90), mar=c(0,0,0,0))
points(visit.x,visit.y, col="red", pch=16)
@Ram-N
Ram-N / visitedCities.csv
Created April 17, 2013 05:24
World Cities with their Long, Lats
City ,Lat-Deg,Lat-Min,Long-Deg,Long-Min,Time
"Aberdeen, Scotland ",57,9 N ,2,9 W ,5:00 p.m.
"Adelaide, Australia ",34,55 S ,138,36 E ,2:30 a.m.1
"Algiers, Algeria",36,50 N ,3,0 E ,6:00 p.m.
"Amsterdam, Netherlands",52,22 N ,4,53 E ,6:00 p.m.
"Ankara, Turkey ",39,55 N ,32,55 E ,7:00 p.m.
"Asuncion, Paraguay ",25,15 S ,57,40 W ,1:00 p.m.
"Athens, Greece",37,58 N ,23,43 E ,7:00 p.m.
"Auckland, New Zealand ",36,52 S ,174,45 E ,5:00 a.m.1
"Bangkok, Thailand ",13,45 N ,100,30 E ,12:00am
@Ram-N
Ram-N / createXYWalls.R
Created April 22, 2013 19:52
Function to get the segments (walls) of cells ready for plotting
#plotting
createXYWalls <- function (cells) {
#let each cell start at its XY position
xs<- NULL; ys<-NULL; xe <- NULL; ye<-NULL
for (i in 1:nrow(cells)) {
if(cells$W[i])
{
xs <- c(xs, cells$x[i]+1)
ys <- c(ys, cells$y[i]+0)
xe <- c(xe, cells$x[i]+1)
@Ram-N
Ram-N / fish_resampling.R
Created May 16, 2013 23:05
How to count number of fish in an odd shaped pond? The Capture-Mark-Recapture technique of estimation Let’s say that we first captured 80, marked them and released them back. (m=80) A few days later, catch 60 (n=60) [sample set] 13 of them are from m. (k=13) N-hat : 80 = 60:13 Okay, very clever. But how good is this estimate? Bootstrapping can g…
BigN<- NULL # BigN is the DISTRIBUTION of N.hats
m <- 80 # initial number of fish that we caught and marked and released
n <- 60 # the fish that were caught again. Our "sample”
for(i in 1:5000) {
s <- sample.int(n, replace=T) #resampling from our second captured set
k <- sum(s<14) # how many of them were the marked ones
N.hat <- (m * n)/k
BigN <- c(N.hat, BigN)
}
# Let's inspect what we got
@Ram-N
Ram-N / gist:5693965
Created June 2, 2013 16:10
Plotting UCBerkeley admissions using ggplot2. See what the different options in ggplot2 do, and how to get your desired graph.
rm(list=ls())
library(datasets)
library(ggplot2)
str(UCBAdmissions) #Table, but ggplot needs data frames
ucb <- data.frame(UCBAdmissions)
names(ucb)
head(ucb)
@Ram-N
Ram-N / facets.R
Created June 21, 2013 18:44
Example to understand how facet_wrap and facet_grid work.
library(ggplot2)
unique(diamonds$cut)
unique(diamonds$clarity)
names(diamonds)
dim(diamonds)
g <- ggplot(diamonds[1:1000,]) + geom_point(aes(x=carat, y=price))
g <- g + facet_grid(. ~ cut) #horizontal stacking
g <- g + facet_grid(cut ~ .) #vertical stacking
g <- g + facet_grid(clarity ~ cut) #vertical stacking
g <- g + facet_wrap(clarity ~ cut) #good for exploring interesting cases
@Ram-N
Ram-N / grouping.R
Last active December 18, 2015 19:39
Learn how to group data for ggplot
library(ggplot2)
unique(diamonds$cut)
unique(diamonds$color)
names(diamonds)
h <- ggplot(diamonds[1:1000,], aes(x=cut)) + geom_bar(); h #simple histogram
h <- ggplot(diamonds[1:1000,], aes(x=cut, fill=color)) + geom_bar(); h # stacked by default
h <- ggplot(diamonds[1:1000,], aes(x=cut, fill=color)) + geom_bar(position="dodge"); h #unstack using dodge
h
@Ram-N
Ram-N / choropleth.R
Created June 27, 2013 00:56
choropleth.R How to create a simple choropleth for all US States. In this simple example, we plot the alphabetically sorted value of the state to a color scale
states_map <- map_data("state")
region <- tolower(state.name)
count.vector <- 1:50 # just a value
df<- data.frame(region, count.vector)
pal <- colorRampPalette(c('grey10','darkgreen'))(50)
mp <- ggplot(df, aes(map_id=region)) + geom_map(aes(fill=count.vector), map=states_map) + coord_map("polyconic") + expand_limits(x = states_map$long, y = states_map$lat)
mp <- mp + scale_fill_gradient(low='grey90', high='darkgreen', limits=c(0,100))
mp