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.
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()
## )
# 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
food_stamps_chart +
geom_line()
food_stamps_chart +
geom_line(size = 1.5, color = "red") +
ggtitle("Line chart")
food_stamps_chart +
geom_line() +
geom_point() +
ggtitle("Dot-and-line chart")
food_stamps_chart +
geom_point() +
ggtitle("Dot-chart")
It works. You get a dot chart.
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
food_stamps_chart +
geom_bar(stat = "identity") +
ggtitle("Column chart")
# 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())
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()
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")
food_stamps_chart +
geom_bar(stat = "identity", color= "white", aes(fill = costs))
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.
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))
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))