####### Installing and loading required packages #######

if (!require("tidyverse")) install.packages("tidyverse")
if (!require("RColorBrewer")) install.packages("RColorBrewer")
if (!require("modeldata")) install.packages("modeldata")

library(tidyverse)
library(RColorBrewer)
library(modeldata)

####### Reading the counties data #######

counties <- read.csv("https://github.com/drkblake/Data/raw/refs/heads/main/counties.csv")

####### Scatterplot of counties broadband vs income #######

ggplot(counties, aes(x = Broadband, y = Income)) +
  geom_point() +
  labs(
    x = "Broadband access",
    y = "Income",
    color = "Urbanization",
    title = "Worker income",
    caption = "Source: U.S. Census Bureau"
  )

####### Exploring crickets dataset #######

data(crickets)  # Loads 'crickets' dataset
View(crickets)  # View in RStudio

# Basic scatterplot
ggplot(crickets, aes(x = temp, y = rate)) + 
  geom_point() +
  labs(
    x = "Temperature",
    y = "Chirp rate",
    title = "Cricket chirps",
    caption = "Source: McDonald (2009)"
  )

# With color by species
ggplot(crickets, aes(x = temp, y = rate, color = species)) + 
  geom_point() +
  labs(
    x = "Temperature",
    y = "Chirp rate",
    color = "Species",
    title = "Cricket chirps",
    caption = "Source: McDonald (2009)"
  ) +
  scale_color_brewer(palette = "Dark2")

# Customizing point properties
ggplot(crickets, aes(x = temp, y = rate)) + 
  geom_point(color = "red", size = 2, alpha = .3, shape = "square") +
  labs(
    x = "Temperature",
    y = "Chirp rate",
    title = "Cricket chirps",
    caption = "Source: McDonald (2009)"
  )

# Adding linear model trendline
ggplot(crickets, aes(x = temp, y = rate)) + 
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    x = "Temperature",
    y = "Chirp rate",
    title = "Cricket chirps",
    caption = "Source: McDonald (2009)"
  )

# Trendline and color by species
ggplot(crickets, aes(x = temp, y = rate, color = species)) + 
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  labs(
    x = "Temperature",
    y = "Chirp rate",
    color = "Species",
    title = "Cricket chirps",
    caption = "Source: McDonald (2009)"
  ) +
  scale_color_brewer(palette = "Dark2") 

# Histogram
ggplot(crickets, aes(x = rate)) + 
  geom_histogram(bins = 15)

# Frequency polygon
ggplot(crickets, aes(x = rate)) + 
  geom_freqpoly(bins = 15)

# Bar plot of species
ggplot(crickets, aes(x = species)) + 
  geom_bar(color = "black", fill = "lightblue")

# Colored bar plot (no legend)
ggplot(crickets, aes(x = species, fill = species)) + 
  geom_bar(show.legend = FALSE) +
  scale_fill_brewer(palette = "Dark2")

# Boxplot of chirp rate by species
ggplot(crickets, aes(x = species, y = rate, color = species)) + 
  geom_boxplot(show.legend = FALSE) +
  scale_color_brewer(palette = "Dark2") +
  theme_minimal()

# Faceted histogram (not great)
ggplot(crickets, aes(x = rate, fill = species)) + 
  geom_histogram(bins = 15) +
  scale_fill_brewer(palette = "Dark2")

# Better faceted histogram
ggplot(crickets, aes(x = rate, fill = species)) + 
  geom_histogram(bins = 15, show.legend = FALSE) + 
  facet_wrap(~species) +
  scale_fill_brewer(palette = "Dark2")

# Vertical faceted layout with minimal theme
ggplot(crickets, aes(x = rate, fill = species)) + 
  geom_histogram(bins = 15, show.legend = FALSE) + 
  facet_wrap(~species, ncol = 1) +
  scale_fill_brewer(palette = "Dark2") +
  theme_minimal()