This is an exploratory analysis of bear sighting data from the Department of Energy & Environmental Protection that contributed to the story: Where bears have been sighted in Connecticut

library(rvest)
library(dplyr)
library(RCurl)
library(scales)
require(rgdal)
require(ggmap)
require(Cairo)
require(gpclib)
require(maptools)
require(reshape)
library(stringr)
library(ggplot2)
library(tidyr)
# Bringing in the data provided by DEEP
burl <- "https://docs.google.com/spreadsheets/d/1iFb5ndUvQqc9adJLsbqPSkZeoU7Fr3Qem7st0qX_6pY/pub?output=csv"
gurl <- getURL(burl)

bear_data <- read.csv(textConnection(gurl))

# Let's take a look at how it looks
head(bear_data)
##           Town Reports
## 1      Ashford       3
## 2         Avon     129
## 3  Barkhamsted     139
## 4 Beacon Falls      11
## 5       Berlin      13
## 6      Bethany      14

Bears by town?

gpclibPermit()
## [1] TRUE
gpclibPermitStatus()
## [1] TRUE
towntracts <- readOGR(dsn="maps", layer="ctgeo")
## OGR data source with driver: ESRI Shapefile 
## Source: "maps", layer: "ctgeo"
## with 169 features
## It has 6 fields
towntracts_only <- towntracts
towntracts <- fortify(towntracts, region="NAME10")

colnames(bear_data) <- c("id", "sightings")

bears_total_map <- left_join(towntracts, bear_data)


dtm <- ggplot() +
  geom_polygon(data = bears_total_map, aes(x=long, y=lat, group=group, fill=sightings), color = "black", size=0.2) +
  coord_map() +
  scale_fill_distiller(type="seq", trans="reverse", palette = "Blues", breaks=pretty_breaks(n=10)) +
  theme_nothing(legend=TRUE) +
  labs(title="Bear sightings by town in Connecticut | 3/15 - 3/16", fill="")

dtm


Calculating bears per capita

# Bringing in my package that can append Connecticut-specific data
library(ctnamecleaner)

bear_data_pop <- ctpopulator(id, bear_data)
## [1] "Checking to see if names match..."
bear_data_pop$percapita <- round((bear_data_pop$sightings/bear_data_pop$pop2013)*1000, 2)

bear_data_pop$id <- str_to_title(bear_data_pop$id)

bears_percapita_map <- left_join(towntracts, bear_data_pop)

#bears_percapita_map <- merge(towntracts, bear_data_pop, by="id", all.x=TRUE)

dtm2 <- ggplot() +
  geom_polygon(data = bears_percapita_map, aes(x=long, y=lat, group=group, fill=percapita), color = "black", size=0.2) +
  coord_map() +
  scale_fill_distiller(type="seq", trans="reverse", palette = "Blues", breaks=pretty_breaks(n=10)) +
  theme_nothing(legend=TRUE) +
  labs(title="Bear sightings per 1,0000 residents in CT | 3/15 - 3/16", fill="")

dtm2

Bear sightings over time

## Bear historical

## Also provided by DEEP
bh <- read.csv("data/bear_history.csv")

bh$Year <- factor(bh$Year)

levels(bh$Month) 
##  [1] "Apr" "Aug" "Dec" "Feb" "Jan" "Jul" "Jun" "Mar" "May" "Nov" "Oct"
## [12] "Sep"
bh$Month <- factor(bh$Month, levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), ordered=TRUE)

ggplot(data=bh, aes(x=Month, y=Sightings, colour=Year, group=Year)) +
  geom_line()

head(bear_data_pop)
##             id sightings pop2013 percapita
## 1      Ashford         3    4301      0.70
## 2         Avon       129   18205      7.09
## 3  Barkhamsted       139    3772     36.85
## 4 Beacon Falls        11    6038      1.82
## 5       Berlin        13   20179      0.64
## 6      Bethany        14    5555      2.52

Conclusions

Read the story Where bears have been sighted in Connecticut