This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
library(lpSolveAPI) | |
#utility functions | |
getColnum <- function(cell, color, edge) { | |
# return(4*(cell-1)+edge) | |
celloffset <- (cell-1)*4*num.colors | |
coloroffset <- (color-1)*4 | |
return(celloffset+coloroffset+edge) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm(list=ls()) | |
library(datasets) | |
library(ggplot2) | |
str(UCBAdmissions) #Table, but ggplot needs data frames | |
ucb <- data.frame(UCBAdmissions) | |
names(ucb) | |
head(ucb) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |