Geometry Types

Scatterplots

Parameters

Aesthetic - Description
x -> X-axis position
y -> Y-axis position
shape -> Shape of each point
color -> Color of each point
size -> Size of each point
alpha -> Transparency of each point

Simple Plot

ggplot(data = college) + 
  geom_point(mapping = aes(x=tuition, y=sat_avg))

Shape

ggplot(data = college) + 
  geom_point(mapping = aes(x=tuition, y=sat_avg, shape = control))

Color, Size & Alpha

ggplot(data = college) + 
  geom_point(mapping = aes(x=tuition, y=sat_avg, color = control, 
                           size = undergrads), alpha = 1/2)

Line

ggplot(data = college) + 
  geom_line(mapping = aes(x=tuition, y=sat_avg, color = control)) +
  geom_point(mapping = aes(x=tuition, y=sat_avg, color = control))

Smooth

ggplot(data = college) + 
  geom_smooth(mapping = aes(x=tuition, y=sat_avg, color = control),  se = FALSE) +
  geom_point(mapping = aes(x=tuition, y=sat_avg, color = control), alpha = 1/5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Bars

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control))

Columns

college %>% 
  group_by(region) %>%
  summarise(average_tuition = mean(tuition)) %>%
  ggplot() +
  geom_col(mapping = aes(x = region, y = average_tuition))

Histograms

ggplot(data = college) + 
  geom_histogram(mapping = aes(x=undergrads), bins = 10, origin = 0)
## Warning: `origin` is deprecated. Please use `boundary` instead.

Boxplot

ggplot(data = college) + 
  geom_boxplot(mapping = aes(x=control, y = tuition))

Beautifying the visualizations

Modyfing the background

Plot.background

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(plot.background = element_rect(fill="purple"))

Panel.background

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_rect(fill="purple"))

No background

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank())

Grid

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank()) +
  theme(panel.grid.major.y = element_line(color = "grey"))

Working with Axis

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank()) +
  theme(panel.grid.major.y = element_line(color = "grey")) + 
  ylab("Number of schools") + 
  xlab("Region") + 
  ylim(0,500)

Working with Axis

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank()) +
  theme(panel.grid.major.y = element_line(color = "grey")) + 
  ylab("Number of schools") + 
  xlab("Region") + 
  ylim(0,500)

Changing scales

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank()) +
  theme(panel.grid.major.y = element_line(color = "grey")) + 
  scale_x_discrete(name = "Region") +
  scale_y_continuous(name = "Number of schools", limits = c(0,500)) +
  scale_fill_manual(values = c("orange", "blue"))

Cleaning up legends

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank()) +
  theme(panel.grid.major.y = element_line(color = "grey")) + 
  scale_x_discrete(name = "Region") +
  scale_y_continuous(name = "Number of schools", limits = c(0,500)) +
  scale_fill_manual(values = c("orange", "blue"),
                    guide = guide_legend(title = "Institution Type", 
                                         nrow = 1, 
                                         label.position = "bottom", 
                                         keywidth = 2)) +
  theme(legend.position = "top")

Annotating Visualizations

ggplot(data = college) + 
  geom_point(mapping = aes(x=tuition, y=sat_avg, color = control, 
                           size = undergrads), alpha = 1/2) +
  annotate("text", label = "Elite Privates", x = 45000, y = 1450) +
  geom_hline(yintercept = mean(college$sat_avg)) + 
  annotate("text", label = "Mean SAT", x = 47500, y = mean(college$sat_avg)-15) + 
  geom_vline(xintercept = mean(college$tuition)) +
  annotate("text", label = "Mean Tuition", x = mean(college$tuition)-7500, y = 700) + 
  theme(panel.background = element_blank(), legend.key = element_blank()) +
  scale_color_discrete(name = "Institution Type") +
  scale_size_continuous(name = "Undergrads") + 
  scale_x_continuous(name = "Tuition") + 
  scale_y_continuous(name = "SAT Score")

Adding tittles

ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) +
  theme(panel.background = element_blank()) +
  theme(plot.background = element_blank()) +
  theme(panel.grid.major.y = element_line(color = "grey")) + 
  scale_x_discrete(name = "Region") +
  scale_y_continuous(name = "Number of schools", limits = c(0,500)) +
  scale_fill_manual(values = c("orange", "blue")) +
  ggtitle("More colleges are in the Southern US than any other Region", 
          subtitle = "Source: U.S. Dept. of Education")

Using Themes

library(ggthemes)
ggplot(data = college) + 
  geom_bar(mapping = aes(x=region, fill = control)) + 
#  theme_bw()
#  theme_minimal() 
  #theme_void()
  #theme_dark()
  theme_solarized()

#theme_excel 
#theme_wsj() 
#theme_economist()

Geospatial Visualizations

Working with maps

## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
#Previuosly,
#library(ggplot2)
#library(ggmap)
#register_google(key = "")

#QuickMap - Plot the Map. 
# qmap("New York, NY", zoom = 18)

#Retrieve the Map 
nyc_map <- get_map("New York, NY", zoom = 18)

ggmap(nyc_map)

Geocoding points

nyc <- geocode("New York, NY")

linda <- geocode("Lynda.com")

whitehouse <- geocode("White House")

ggmap(get_map(linda))

Changing Map types

ggmap(get_map(nyc, maptype = "roadmap"))

#Others maptype: terrain, roadmap, terrain-labels, terrain-lines, sattelite, hybrid, toner, toner-lite, toner-blackground, watercolor.

Plotting points on a Map

One point in a Map

usa <- geocode("United States")
nyc <- geocode("New York, NY")


ggmap(get_map(usa, zoom = 4)) + 
  geom_point(mapping = aes(x=lon, y=lat), color = "red", data=nyc)

Some points in a Map

library(tibble)

placesnames <- c("New York, NY", "Lynda.com", "White House", "Mt. Rushmore",
                 "The alamo")

locations <- geocode(placesnames)

places <- tibble(name = placesnames, lat = locations$lat, lon = locations$lon)

ggmap(get_map(usa, zoom = 4, maptype = "watercolor")) + 
  geom_point(mapping = aes(x=lon, y=lat), color = "red", data=places) + 
  geom_text(mapping = aes(x=lon, y=lat, label= name), color = "red", data = places, nudge_y = 1)

Building a Map Manually

states <- map_data("state")

ggplot(states, aes(long, lat, group = group))+
  geom_polygon() +
  coord_map() +
  theme(axis.title = element_blank(),
        axis.text = element_blank(),
        panel.background = element_blank())

Choropleth Map

library(dplyr)
library(stringr)

college <- read.csv('http://672258.youcanlearnit.net/college.csv')
college <- college %>% 
  mutate(state = as.factor(state), region = as.factor(region), 
         highest_degree = as.factor(highest_degree), 
         control = as.factor(control), gender = as.factor(gender), 
         loan_default_rate = as.numeric(loan_default_rate)) 

states <- map_data("state")

college_summary <- college %>% 
  group_by(state) %>%
  summarize(schools=n())

college_summary <- college_summary %>%
  mutate(region=as.character(setNames(str_to_lower(state.name), 
                                      state.abb)[as.character(state)]))


college_summary <- college_summary %>%
  mutate(region=ifelse(as.character(state)== "DC", "disctrict of columbia", region))

map_data <- merge(states, college_summary, by= "region")

ggplot(map_data) +
  geom_polygon(mapping = aes(x = long, y= lat, group=group, fill= schools)) +
  coord_map() +
  theme(plot.background = element_blank(),
        panel.background = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank()) +
  scale_fill_gradient(low= "beige", high="red")