Week 3 Assignment

Created on 2017-02-08.

Murder Rate per 100,000 in 1973

data(USArrests)


USArrests$State <- row.names(USArrests)
USArrests$StateAbb <- state.abb[match(USArrests$State, state.name)]
USArrests$Hover <- with(USArrests, paste(State, '<br>Urban Pop:', UrbanPop, '%<br>Murder:', Murder, '<br>Assault:', Assault, '<br>Rape:', Rape))

#state_data <- data.frame(State = row.names(USArrests), Pop = as.vector(state.x77[,1]))
# Create hover text
#state_data$hover <- with(state_pop, paste(State, '<br>', "Population:", Pop))
# Make state borders white
borders <- list(color = toRGB("red"))
# Set up some mapping options
map_options <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)
plot_ly(z = ~USArrests$Murder, text = ~USArrests$Hover, locations = ~USArrests$StateAbb, 
        type = 'choropleth', locationmode = 'USA-states', 
        color = USArrests$Murder, colors = 'Reds', marker = list(line = borders)) %>%
  layout(title = 'Murder rate per 100k in 1973', geo = map_options)
## Warning in arrange_impl(.data, dots): '.Random.seed' is not an integer
## vector but of type 'NULL', so ignored

Relating Crime rate to % Urban Population

#colors = colors, 

plot_ly(USArrests, x = ~Murder, y = ~Assault, z = ~Rape, color = ~UrbanPop, size = ~UrbanPop, 
             marker = list(symbol = 'circle', sizemode = 'diameter'), sizes = c(5, 20),
             text = ~Hover) %>%
  layout(title = 'Urban Population by Crime Rate',
         scene = list(xaxis = list(title = 'Murder Rate (per 100k)',
                      gridcolor = 'rgb(255, 255, 255)',
                      range = c(0.800, 17.400),
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwidth = 2),
               yaxis = list(title = 'Assault Rate (per 100k)',
                      gridcolor = 'rgb(255, 255, 255)',
                      range = c(45.0, 337.0),
                      zerolinewidth = 1,
                      ticklen = 5,
                      gridwith = 2),
               zaxis = list(title = 'Rape Rate (per 100k)',
                            gridcolor = 'rgb(255, 255, 255)',
                            zerolinewidth = 1,
                            ticklen = 5,
                            gridwith = 2)),
         paper_bgcolor = 'rgb(243, 243, 243)',
         plot_bgcolor = 'rgb(243, 243, 243)')
## No trace type specified:
##   Based on info supplied, a 'scatter3d' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter3d
## No scatter3d mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode