library(fansi)
library (ggplot2)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v tibble  3.1.2     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

#Make a series of charts from food stamps data

Now we will explore a series of other geom functions using the food stamps data.

Load the data,

setwd("C:/Documents - Copy/PERSONAL/Data 110_MC_Class")
food_stamps <- read_csv("food_stamps.csv")
## 
## -- Column specification --------------------------------------------------------
## cols(
##   year = col_double(),
##   participants = col_double(),
##   costs = col_double()
## )

Map variables onto the X and Y axes, and save chart template

# save basic chart template
food_stamps_chart <- ggplot(food_stamps, aes(x = year, y = participants)) + 
  labs(title = "Food Stamps Participation Over the Years") +
       xlab("Year") +
       ylab("Participants (millions)") +
  theme_minimal(base_size = 14)
food_stamps_chart

Make a line chart using the geom-line function

food_stamps_chart +  
  geom_line()

Customize the line (making the line thick and red in color), and add a title

food_stamps_chart +
  geom_line(size = 1.5, color = "red") +
  ggtitle("Line chart")

Add a second layer (using geom-point) to make a dot-and-line chart

food_stamps_chart +
  geom_line() +
  geom_point() +
  ggtitle("Dot-and-line chart")

What if you started wiht geom-point

food_stamps_chart +
   geom_point() +
  ggtitle("Dot-chart")

It works. You get a dot chart.

What if you add color =red.

food_stamps_chart +
  geom_line(color="red") +
  geom_point() +
  ggtitle("Dot-and-line chart")

Try coloring the dots green

food_stamps_chart +
  geom_line(color="red") +
  geom_point(color="green") +
  ggtitle("Dot-and-line chart")

Make a column chart

# Make a column chart
food_stamps_chart +
  geom_bar(stat = "identity") +
  ggtitle("Column chart") 

Customize the theme

# Make a column chart
food_stamps_chart +
  geom_bar(stat = "identity") +
  ggtitle("Column chart") +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank())

coord_flip switches the X and Y axes.

food_stamps_chart +
  geom_bar(stat = "identity") +
  ggtitle("Bar chart") +
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
  coord_flip()

The difference between color and fill

For some geoms, notably geom_bar, you can set color for their outline as well as the interior of the shape. When setting colors, color refers to the outline, fill to the interior of the shape.

food_stamps_chart +
  geom_bar(stat = "identity", color = "#888888", fill = "#CCCCCC", alpha = 0.5) +
  ggtitle("Column chart")

Map color to the values of a continuous variable

food_stamps_chart +
  geom_bar(stat = "identity", color= "white", aes(fill = costs))

Use a ColorBrewer sequential color palette

food_stamps_chart +
  geom_bar(stat = "identity", color = "#888888", aes(fill = costs)) +
  scale_fill_gradient(name = "Cost\n($ billion)", low = "#d1dee8", high = "#d92774")

Notes: scale_fill_distiller (and scale_color_distiller) work like scale_color_brewer, but set color gradients for ColorBrewer’s sequential and diverging color palettes; direction = 1 ensures that larger numbers are mapped to more intense colors (direction = -1 reverses the color mapping). Try changing the code I have: scale_fill_gradient() to scale_fill_distiller with different directions (1 or -1).Notice also the in the title for the legend. This introduces a new line.

Control the position of the legend (using the theme function: legend_position, identify posiiton using coordinates)

This code uses the theme function to moves the legend from its default position to the right of the chart to use some empty space on the chart itself.

food_stamps_chart +
  geom_bar(stat="identity", color = "#888888", aes(fill=costs)) +
  scale_fill_gradient(name = "Cost\n($ billion", low = "#d1dee8", high = "#d92774") +
  theme(legend.position=c(0.15,0.8))

Try a different position

food_stamps_chart +
  geom_bar(stat="identity", color = "#888888", aes(fill=costs)) +
  scale_fill_gradient(name = "Cost\n($ billion", low = "#d1dee8", high = "#d92774") +
  theme(legend.position=c(0.5,0.8))