Maps
1.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.
data(USArrests)
head(USArrests)
Murder Assault UrbanPop Rape
Alabama 13.2 236 58 21.2
Alaska 10.0 263 48 44.5
Arizona 8.1 294 80 31.0
Arkansas 8.8 190 50 19.5
California 9.0 276 91 40.6
Colorado 7.9 204 78 38.7
library(maps);library(ggplot2)
Warning: package 'maps' was built under R version 3.2.5
# maps v3.1: updated 'world': all lakes moved to separate new #
# 'lakes' database. Type '?world' or 'news(package="maps")'. #
all_states <- map_data("state")
head(all_states)
long lat group order region subregion
1 -87.46201 30.38968 1 1 alabama <NA>
2 -87.48493 30.37249 1 2 alabama <NA>
3 -87.52503 30.37249 1 3 alabama <NA>
4 -87.53076 30.33239 1 4 alabama <NA>
5 -87.57087 30.32665 1 5 alabama <NA>
6 -87.58806 30.32665 1 6 alabama <NA>
USArrests$region <- row.names(USArrests)
USArrests$region <-tolower(USArrests$region)
Total <- merge(all_states, USArrests, by="region")
head(Total)
region long lat group order subregion Murder Assault UrbanPop
1 alabama -87.46201 30.38968 1 1 <NA> 13.2 236 58
2 alabama -87.48493 30.37249 1 2 <NA> 13.2 236 58
3 alabama -87.95475 30.24644 1 13 <NA> 13.2 236 58
4 alabama -88.00632 30.24071 1 14 <NA> 13.2 236 58
5 alabama -88.01778 30.25217 1 15 <NA> 13.2 236 58
6 alabama -87.52503 30.37249 1 3 <NA> 13.2 236 58
Rape
1 21.2
2 21.2
3 21.2
4 21.2
5 21.2
6 21.2
ggplot(data = Total, aes(x = long, y = lat, group = group,fill = Murder)) +
geom_polygon(colour="white") +
scale_fill_continuous(low = "pink", high = "red", guide="colorbar") +
labs(x = " ",y = " ") +
ggtitle("Murder Rates by US States in 1973\n(arrests per 100,000 residents)") +
theme_minimal() +
theme(axis.text.x = element_blank(),axis.text.y = element_blank(),
panel.background = element_blank(),panel.grid = element_blank() )
3.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.
library(choroplethrAdmin1)
Warning: package 'choroplethrAdmin1' was built under R version 3.2.5
library(ggplot2)
# create data
tw <- get_admin1_map("taiwan")
death <- data.frame(c("taipei","t'ai-pei shih","taichung","tainan","kao-hsiung hsien",
"yilan", "taoyuan","hsinchu","miaoli","changhua",
"nantou","yunlin","chiayi","pingtung","taitung","hualien",
"penghu","keelung","hsinchu"))
death$rate <- 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)
names(death) <- c("region","rate")
head(death)
region rate
1 taipei 0.44
2 t'ai-pei shih 0.53
3 taichung 0.79
4 tainan 1.02
5 kao-hsiung hsien 0.81
6 yilan 1.77
# plot
ggplot(data=death,aes(map_id =region)) +
geom_map(aes(fill = rate),map = tw,col="white") +
expand_limits(x = tw$long, y = tw$lat) +
scale_fill_continuous(low = "gray", high = "black", guide="colorbar") +
ggtitle("No. of Death People per 10,000 Vehicles") +
theme_minimal() +
labs(x = " ",y = " ")
5.Plot places in administratice areas of Taiwan you have visited so far.
library(choroplethrAdmin1)
library(ggplot2)
tw_map <- get_admin1_map("taiwan")
tw_map$visit <- ifelse(tw_map$region %in% c("kao-hsiung hsien","pingtung","tainan","hsinchu","yilan",
"keelung","t'ai-pei shih","taipei","taoyuan","chiayi","hualien" ,
"nantou","taichung" ,"taitung"), 1 , 0)
tw_map$visit <- as.factor(tw_map$visit)
head(tw_map)
long lat admin region group order hole piece
833009 121.0193 23.43668 taiwan kao-hsiung hsien 4187.1 833009 FALSE 1
833010 121.0265 23.41640 taiwan kao-hsiung hsien 4187.1 833010 FALSE 1
833011 121.0152 23.39904 taiwan kao-hsiung hsien 4187.1 833011 FALSE 1
833012 120.9939 23.39036 taiwan kao-hsiung hsien 4187.1 833012 FALSE 1
833013 120.9887 23.37919 taiwan kao-hsiung hsien 4187.1 833013 FALSE 1
833014 120.9924 23.36534 taiwan kao-hsiung hsien 4187.1 833014 FALSE 1
id visit
833009 4187 1
833010 4187 1
833011 4187 1
833012 4187 1
833013 4187 1
833014 4187 1
ggplot(tw_map, aes(long, lat, group = group)) +
geom_polygon(aes(fill = visit), color = "gray") +
scale_fill_manual(values=c("white","pink"),labels=c(0,1)) +
theme_minimal()
Dates
1.Assume that your total life span is 100 years. Find out how often your birthday falls on each day of the week.
w <- weekdays(seq(as.Date("1994/04/01"), by = "1 years", length =100))
table(w)
w
星期一 星期二 星期三 星期五 星期六 星期日 星期四
14 15 14 14 15 14 14
3.Find out the number of days you have spent at NCKU as a registered student.
register <- "2012/09/17"
register <- as.Date(register, format="%Y/%m/%d")
today <- Sys.Date()
difftime(today, register, units = "days")
Time difference of 1331 days