The built-in dataset USArrests in R contains the arrests rate for murders by state for the U.S. in 1973. Reproduce the map below.
Source: Mittal, H.V. (2011). R Graphs Cookbook. Packt Publishing.
states <- map_data("state")
arrests <- USArrests
names(arrests) <- tolower(names(arrests))
arrests$region <- tolower(rownames(USArrests))
choro <- merge(states, arrests, sort = FALSE, by = "region")
choro <- choro[order(choro$order), ]
ggplot(choro, aes(long, lat)) +
geom_polygon(aes(group = group, fill = murder),col="white") +
scale_fill_distiller( palette = "Reds",direction=1)+
ggtitle("Murder Rates by US States in 1973 \n (arrests per 100,000 residents)")+
xlab("") +
ylab("") +
theme_classic()+
theme(axis.text=element_blank())
Build a thematic plot of the results of Taiwan 2016 presidential election between the DDP and the KMT. The geographical data (maps) for Taiwan can be obtained from DIVA-GIS: Geographic Information System for Biodiversity Research.
Data - Taiwan Administrative units
Source: Taiwan presidential election 2016 . Wikipedia.
taiwan_shp <- readShapePoly("K:/Dropbox/1042_dataM/Project/TWmaps/TWN_adm2.shp")
tw<-fortify(taiwan_shp,region = "NAME_2")
tnm<-taiwan_shp@data$NAME_2
res<-cbind(as.matrix(tnm),rep('DPP',length(tnm)))
kmt<-c("Taitung","Hualien","Kinmen","Lienkiang (Matsu Islands)")
res[(res[,1]==kmt[1]|res[,1]==kmt[2]|res[,1]==kmt[3]|res[,1]==kmt[4]),2]<-"KMT"
colnames(res)<-c("id","resp")
res<-as.data.frame(res)
dta<-merge(tw,res,sort=F,by="id")
ggplot(dta,aes(long, lat, group = group)) +
geom_polygon(aes(group = group, fill = resp),color ="White" ) +
scale_fill_manual(values = c("darkgreen","blue"))+
theme_minimal()
Traffic accidents on roads in Taiwan in 2011 is available on-line from the Department of Transportation. Plot the number of deaths per 10,000 vehicles over the administrative units.
death<-c(0.44,0.53,0.79,1.02,0.81,1.77,0.71,1.69,1.04,1.04,1.33,1.69,1.60,1.44,2.28,1.47,0.75,0.67,0.89,0.55,0,0)
id<-c("Taipei","New Taipei City","Taichung","Tainan","Kaohsiung",
"Yilan","Taoyuan","Hsinchu County","Miaoli","Changhua","Nantou",
"Yulin","Chiayi County","Pingtung","Taitung","Hualien","Penghu",
"Keelung","Hsinchu City","Chiayi City","Kinmen","Lienkiang (Matsu Islands)")
death.dta<-cbind(id,death)
dta<-merge(tw,death.dta,sort=F,by="id")
dta$death<-as.numeric(as.character(dta$death))
ggplot(dta,aes(long, lat, group = group)) +
geom_polygon(aes(group = group, fill = death),color ="White" ) +
scale_fill_distiller( palette = "OrRd",direction=1)+
ggtitle("number of deaths per 10,000 vehicles")+
theme_minimal()
Indicate countries you have visited so far on a world map in the style of the ebola outbreaks example.
library(rworldmap)
dta <- as.data.frame(matrix(data=c("Taiwan","Thailand","China","Cambodia","Malaysia"),dimnames=list(c(1:5),c("Country")),nrow=5,byrow=T))
library(countrycode)
# a data.frame with the ISO3 country names plus a variable to
# merge to the map data
dta$Country <- countrycode(dta[,1], "country.name", "iso3c")
dta$Visited <- rep(1, length(dta$Country))
mapDevice("x11")
# join the data.frame to the country map data
ebolaMap <- joinCountryData2Map(dta, joinCode = "ISO3", nameJoinColumn = "Country")
# plot it, the color palette's first color is red
mapCountryData(ebolaMap, nameColumnToPlot = "Visited", catMethod = "categorical",
addLegend = FALSE, mapTitle =" ", missingCountryCol = gray(.9))
dev.off()
###
Plot places in administratice areas of Taiwan you have visited so far.
ggplot(tw,aes(long, lat, group = group)) +
geom_polygon(fill="orchid",color ="White") +
ggtitle("administratice areas of Taiwan I have visited so far")+
theme_minimal()
Map an area of Tainan city to include three of your favorite places to eat as landmarks
m <- leaflet() %>%
addTiles() %>%
addMarkers(lat=23.003989, lng=120.218911, popup ="勝利路7-11") %>%
addMarkers(lat=23.001867, lng=120.218095, popup ="成大星巴克") %>%
addMarkers(lat=22.992017, lng=120.221894, popup ="東寧路摩斯")
m
Assume that your total life span is 100 years. Find out how often your birthday falls on each day of the week.
dta<-table(weekdays(seq(as.Date('1990-02-12'), by = "1 years", length = 100)))
dta[c(1:3,7,4:6)]
星期一 星期二 星期三 星期四 星期五 星期六 星期日
15 14 15 14 14 15 13
Replicate the analysis of air passengers data set presented in the lecture note with the data set on the number of births per month in New York City from January 1946 to December 1958.
dta<-ts(read.table('http://robjhyndman.com/tsdldata/data/nybirths.dat'),frequency = 12,start=c(1946,1))
plot(dta, xlab = "Date", ylab = "births per month")
grid()
xyplot(dta, type = c("p", "g", "r", "l"), xlab = "Date (Month-Year)",
ylab = "births per month")
boxplot(dta ~ as.factor(cycle(dta)), xlab = "Month",
ylab = "births per month")
abline(h = mean(dta), col = "red", lty = 3)
Find out the number of days you have spent at NCKU as a registered student.
difftime(Sys.Date(), as.Date("2013-09-01"), units = "days")
Time difference of 986 days
Reproduce the plot of calls for police assistances around 24 hours in New York City using the data set here.
dta<-read.csv('http://titan.ccunix.ccu.edu.tw/~psycfs/dataM/Data/calls_nyc.csv')
ggplot(data = dta, aes(x = Hour, y = Calls, group = 1)) +
geom_bar(width = 1, stat = "identity", fill = "cyan",
color = "gray", alpha = 0.2) +
geom_abline(intercept = mean(dta$Calls), slope = 0,
size = 1, color = "lightpink") +
coord_polar(theta = "x", start = -pi/24) +
theme_bw()