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
p1<-ggplot(data=PE) +
geom_point(aes(x=year,y=demVote,color=south),size=2)
p1
Use xlab(), ylab(), and ggtitle().
p2<-p1+xlab("")+ylab("% Democratic")+ggtitle("USA Change in Democratic Vote")
p2
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'
By default, the functon uses ‘loess’ method. If you would like to use another method, such as lm, you can change the method argument.
p2+geom_smooth(aes(x=year,y=demVote,color=as.factor(south)), method = 'lm')
## `geom_smooth()` using formula 'y ~ x'
You can also change the smooth curve by formula argument, such as adding higher-order terms.
p2+geom_smooth(aes(x=year,y=demVote,color=as.factor(south)), method = 'lm', formula = y ~ poly(x, 2))
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'
You can also modify the names of each category in the legend.
p3+ guides(color=guide_legend(title=""))+
scale_color_discrete(labels = c("North", "South"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
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'
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.
For the color aesthetic, we want to manually select the two different colors.
p6<-p5+geom_vline(xintercept=1957,alpha=0.8,linetype=4)
p6
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Use theme_minimal() for the plot.
FINALPLOT1<-p6+theme_minimal()
FINALPLOT1
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
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.
p1<-ggplot(data=PE[PE$year==1932,]) +
geom_density(aes(x=demVote,fill=south))
p1
Modify the legend, titles, color, theme similarly to how we did the first plot. 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
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
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)
## [,1] [,2]
## [1,] 1 2
## [2,] 1 3
LAYOUT=matrix(c(1,1,2,3),ncol=2)
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3,layout_matrix=LAYOUT)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
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'
You can include holes with missing value in matrix.
LAYOUT=rbind(c(1,1,1),
c(2,NA,3))
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3,layout_matrix=LAYOUT)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'