##### Graphics Script ### SlideDeck b2 ### Numeric graphics ### Preamble # Load additional functions source("https://rfs.kvasaheim.com/stat200.R") # Load and attach the crime dataset dt = read.csv("http://rfs.kvasaheim.com/data/crime.csv") attach(dt) ### Histograms # Default hist(vcrime00) # breaks=11 hist(vcrime00, breaks=11) # breaks=5 hist(vcrime00, breaks=5) # red shading; specific break values # seq(0,2000,100) is equivalent to: # 0, 100, 200, ..., 1900, 2000 par(cex.axis=1.1, mar=c(4,3,1,1) ) hist(vcrime00, breaks=seq(0,2000,100), col=rgb(0:20/20,0,0), border="white" ) ### Density Plots # default plot(density(vcrime00)) # overlaying the red histogram hist(vcrime00, freq=FALSE, breaks=seq(0,2000,100), col=rgb(0:20/20,0,0), border="white" ) lines(density(vcrime00), lwd=5, col="white") lines(density(vcrime00), lwd=2) # overlaying a blue-themed histogram # with no base spacing par(mar=c(4,1,1,1) ) par(yaxt="n", family="serif") par(cex.lab=1.2,font.lab=2) par(yaxs="i") hist(vcrime00, freq=FALSE, breaks=seq(0,2000,100), col=rgb(0,0,0:20/20), border="white", main="", xlab="" ) lines(density(vcrime00), lwd=5, col="white") lines(density(vcrime00), lwd=2, col="green") title(xlab="Violent Crime Rate (2000)") abline(h=0, lwd=3) ### Boxplot # default boxplot(vcrime00) # horizontal boxplot(vcrime00, horizontal=TRUE) # bivariate (vcrime00 against census4 region) boxplot(vcrime00~census4) # as horizontal par(mar=c(4,6,1,1) ) boxplot(vcrime00~census4, horizontal=TRUE, las=1, ylab="") # as horizontal with names par(mar=c(4,6,1,1), font.lab=2 ) boxplot(vcrime00~census4, horizontal=TRUE, las=1, ylab="", xlab="Violent Crime Rate (2000)") st= 4; text(vcrime00[st],2,label=scode[st], cex=0.8, pos=1) # MA st=24; text(vcrime00[st],3,label=scode[st], cex=0.8, pos=1) # DC st=49; text(vcrime00[st],4,label=scode[st], cex=0.8, pos=1) # CA st=50; text(vcrime00[st],4,label=scode[st], cex=0.8, pos=3) # AK ### Scatter plots # default plot(vcrime00 ~ gspcap90) # with weird symbol used (pch=14) plot(vcrime00 ~ gspcap90, pch=14) # final scatter plot with several bells and whistles par(mar=c(4,4,1,1)) par(family="serif", font.lab=2) par(cex.lab=1.2, cex.axis=0.8) par(xaxs="i", yaxs="i") par(las=1) plot.new() plot.window( xlim=c(0,70000), ylim=c(0,2000) ) abline(lm(vcrime00~gspcap90), lwd=3, col="purple") abline(lm(vcrime00~gspcap90), lwd=1, col="pink") points(gspcap90,vcrime00, pch=21, bg="lightblue") axis(1) axis(2) title(xlab="GSP per Capita (1990)") title(ylab="Violent Crime Rate (2000)") dt # <--- which are the two high gspcap90 values? st=24; text(gspcap90[st],vcrime00[st], label=scode[st], pos=3) # <- DC st=50; text(gspcap90[st],vcrime00[st], label=scode[st], pos=1) # <- Alaska # print the correlation r = round(cor(gspcap90,vcrime00),4) text(60000,0, label=paste("r =",r), pos=3, cex=0.8) ##### ### Script for the First Two Maps # New package # install.packages("plotly") ## <-- uncomment if you already have this package library(plotly) detach(dt) dt = read.csv("http://rfs.kvasaheim.com/data/crime.csv", colClasses = c("ccode"="character")) colnames(dt)[4] = "code" dt$census4colors = as.numeric(as.factor(dt$census4)) attach(dt) # Map 1 l <- list(color = toRGB("white"), width = 2) g <- list( scope = 'usa', projection = list(type = 'albers usa'), showlakes = TRUE, lakecolor = toRGB('white') ) ### CENSUS4 fig <- plot_geo(dt, locationmode = 'USA-states') fig <- fig %>% add_trace( z = ~census4colors, text = ~state, locations = ~code, color = ~census4colors, colors = 'Reds' ) fig <- fig %>% hide_colorbar() fig <- fig %>% layout( geo = g ) fig ### VCRIME00 fig <- plot_geo(dt, locationmode = 'USA-states') fig <- fig %>% add_trace( z = ~vcrime00, text = ~state, locations = ~code, color = ~vcrime00, colors = 'Purples' ) fig <- fig %>% colorbar(title = "Crime Rate (2000)") fig <- fig %>% layout( geo = g ) fig ### ##### ### Aside: Graphic of the available pch numbers plot.new() plot.window( xlim=c(1,13), ylim=c(-0.5,1.5) ) for(i in 1:13) { points(i, 0, pch=i, bg="pink", cex=2) text( i, -0.1, label=i, pos=1) } for(i in 1:12) { points(i, 1, pch=13+i, bg="pink", cex=2) text( i, 0.9, label=13+i, pos=1) } ### End of File