In this lab, 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.
You will need to modify the code chunks so that the code works within
each of chunk. When you get the desired result for each step, change
eval=F
to eval=T
and knit the document to HTML
to make sure it works. Do not work out of order or skip around. After
you complete the lab or by the end of class, you should submit your HTML
file of what you have completed to Canvas.
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=as.factor(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(COMPLETE)
p3
Since the legend is for the color aesthetic we use
guides(color=guide_legend(title=COMPLETE_INSIDE))
to rename
the legend.
p4<-p3+ COMPLETE
p4
For the color aesthetic, we want to manually select the two different colors.
p5<-p4+ scale_color_manual(values=c("COLOR 1","COLOR 2"))
p5
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(MISSING_OPTION,alpha=0.8,linetype=4)
p6
Use theme_minimal()
for the plot.
FINALPLOT1<-EDIT_INFORMATION
FINALPLOT1
PE$year
is a vector of all the years represented in the
data for all states. PE$year==1932
creates 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(COMPLETE) + ylab(COMPLETE) + etc......
FINALPLOT2
Repeat the code that created FINALPLOT2 for the year 2016. I advise copy and paste.
FINALPLOT3<- .......
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. After knitting the document and observing the picture,
change the code chunk option fig.width=4
to
fig.width=8
and see what happens.
#First Examine this Code and See What Happens
#Defaults to 1 Column Layout and Stacks Plots
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3)
#Starts Placing Plots in a Two Column Layout
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3,ncol=2)
#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)
LAYOUT=matrix(c(1,1,2,3),ncol=2)
grid.arrange(FINALPLOT1,FINALPLOT2,FINALPLOT3,layout_matrix=LAYOUT)