Step-by-Step Image Building

In this tutorial, we will slowly construct the image below via ggplot. This chart was created using the presidentialElections dataset in the pscl package. These plots visually compare the historical change in the Democratic Vote between the former Confederate states and non-Confederate states.

For ease, we start by reassigning the dataset presidentialElections to a new variable called PE.

PE=presidentialElections
head(PE,5)
## # A tibble: 5 × 4
##   state      demVote  year south
##   <chr>        <dbl> <int> <lgl>
## 1 Alabama       84.8  1932 TRUE 
## 2 Arizona       67.0  1932 FALSE
## 3 Arkansas      86.3  1932 TRUE 
## 4 California    58.4  1932 FALSE
## 5 Colorado      54.8  1932 FALSE

Plot 1: Time Series Plot

Step 1: Initiate Plot and Print

p1<-ggplot(data=PE) +
  geom_point(aes(x=year,y=demVote,color=south),size=2)
p1

Step 2: Modify the Title and Labels

Use xlab(), ylab(), and ggtitle().

p2<-p1+xlab(" ")+ylab("% Democratic")+ggtitle("USA Change in Democratic Vote")
p2

Step 3: Get Smooth Curves and Print

Use geom_smooth similarly how we used geom_point.

p3<-p2+geom_smooth(aes(x=year,y=demVote,color=south))
p3
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Step 4: Modify the Legend Title and Print

Since the legend is for the color aesthetic we use guides(color=guide_legend(title=COMPLETE_INSIDE)) to rename the legend.

p4<-p3+guides(color=guide_legend(title='South'))
p4
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Step 5: Manually Select the Colors and Print

For the color aesthetic, we want to manually select the two different colors.

p5<-p4+ scale_color_manual(values=c("blue","red"))
p5
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Step 6: Reference Line and Print

It doesn’t seem to be until around 1957 where the non-Confederate states began to exceed the former Confederacy on approval for the Democratic party. We want to create a vertical line through the x-axis at 1957 using geom_vline. Check ?geom_vline for more information about this geometric object.

p6<-p5+geom_vline(aes(xintercept=1957),alpha=0.8,linetype=4)
p6
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Step 7: Modify the Theme and Print

Use theme_minimal() for the plot.

FINALPLOT1<-p6 + theme_minimal()
FINALPLOT1
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Plot 2: Overlapping Density Curves in 1932

Step 1: Create Basic Density Plot

PE$year is a vector of all the years represented in the data for all states. PE$year==1932creates a vector of TRUE and FALSE where TRUE indicates observations from the year 1932. PE[PE$year==1932,] modifies the dataset to only include the data for the year 1932.

Create density plot with geom_density and fill of the density curves is controlled by south.

p1<-ggplot(data=PE[PE$year==1932,]) +
      geom_density(aes(x=demVote,fill = south))
p1

Step 2: Modify Plot and Print

Modify graph to complete it. Also, add a vertical line at 50 which indicates the transition to a majority vote.

FINALPLOT2<-p1 + xlab("% Democratic")+ylab("Density")+
  ggtitle("Distribution of Democratic Vote from 1932")+
  scale_fill_manual(values=c("blue","red"))+
  guides(fill=guide_legend(title='South'))+
  geom_vline(xintercept=50)+
  theme_minimal()
  
FINALPLOT2

Plot 3: Overlapping Density Curves in 2016

Repeat the code that created FINALPLOT2 for the year 2016. I advise copy and paste.

FINALPLOT3<- ggplot(data=PE[PE$year==2016,]) +
      geom_density(aes(x=demVote,fill = south)) +
 xlab("% Democratic")+ylab("Density")+
  ggtitle("Distribution of Democratic Vote from 2016")+
  scale_fill_manual(values=c("blue","red"))+
  guides(fill=guide_legend(title='South'))+
  geom_vline(xintercept=50)+
  theme_minimal() 
FINALPLOT3

Arrange All Plots in a Pleasant Layout

The grid.arrange() function from the gridExtra package allows us to do this. This needs to be done after all three plots are created. See if you can figure out what is going on

#First Examine this Code and See What Happens

#Defaults to 1 Column Layout and Stacks Plots
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3) 
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

#Starts Placing Plots in a Two Column Layout
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3,ncol=2) 
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

#Now Check this Code Out: Try to Understand how the Matrix is Created and How the Layout is Controlled by the Matrix. Modify It to Get What I created
matrix(c(1,1,2,3),ncol=2,byrow=T)
##      [,1] [,2]
## [1,]    1    1
## [2,]    2    3
LAYOUT=matrix(c(1,2,1,3),ncol=2)
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3,layout_matrix=LAYOUT)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'