library(reshape2)
library(knitr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
##
## Attaching package: 'tidyr'
## The following object is masked from 'package:reshape2':
##
## smiths
library(digest)
## Warning: package 'digest' was built under R version 3.3.3
library(Rcpp)
## Warning: package 'Rcpp' was built under R version 3.3.3
library(ggplot2)
library(ggvis)
##
## Attaching package: 'ggvis'
## The following object is masked from 'package:ggplot2':
##
## resolution
Votes <- read.csv("Votes.csv")
kable(Votes)
| Trump |
0.46 |
0.43 |
0.41 |
0.46 |
0.49 |
0.60 |
0.58 |
| Clinton |
0.47 |
0.49 |
0.50 |
0.46 |
0.45 |
0.35 |
0.39 |
| Other |
0.07 |
0.08 |
0.09 |
0.08 |
0.06 |
0.05 |
0.03 |
new <- melt(Votes, value.name = "frequency")
## Using Candidate as id variables
new <- new %>% rename(Loss_Gain = variable, Support_for_Candidate = frequency)
new <- new %>% select(If.You.Stand.To = Loss_Gain, You.Voted.For = Candidate, Percent.Of.The.Time = Support_for_Candidate)
kable(new)
| Gain_2500_or_Greater |
Trump |
0.46 |
| Gain_2500_or_Greater |
Clinton |
0.47 |
| Gain_2500_or_Greater |
Other |
0.07 |
| Gain_1000.2500 |
Trump |
0.43 |
| Gain_1000.2500 |
Clinton |
0.49 |
| Gain_1000.2500 |
Other |
0.08 |
| Gain_1000_or_Less |
Trump |
0.41 |
| Gain_1000_or_Less |
Clinton |
0.50 |
| Gain_1000_or_Less |
Other |
0.09 |
| Lose_between_1000_2500 |
Trump |
0.46 |
| Lose_between_1000_2500 |
Clinton |
0.46 |
| Lose_between_1000_2500 |
Other |
0.08 |
| Lose_between_2500_5000 |
Trump |
0.49 |
| Lose_between_2500_5000 |
Clinton |
0.45 |
| Lose_between_2500_5000 |
Other |
0.06 |
| Lose_between_5000_7500 |
Trump |
0.60 |
| Lose_between_5000_7500 |
Clinton |
0.35 |
| Lose_between_5000_7500 |
Other |
0.05 |
| Lose_more_than_7500 |
Trump |
0.58 |
| Lose_more_than_7500 |
Clinton |
0.39 |
| Lose_more_than_7500 |
Other |
0.03 |
new<- new[seq(dim(new)[1],1),]
kable(new)
| 21 |
Lose_more_than_7500 |
Other |
0.03 |
| 20 |
Lose_more_than_7500 |
Clinton |
0.39 |
| 19 |
Lose_more_than_7500 |
Trump |
0.58 |
| 18 |
Lose_between_5000_7500 |
Other |
0.05 |
| 17 |
Lose_between_5000_7500 |
Clinton |
0.35 |
| 16 |
Lose_between_5000_7500 |
Trump |
0.60 |
| 15 |
Lose_between_2500_5000 |
Other |
0.06 |
| 14 |
Lose_between_2500_5000 |
Clinton |
0.45 |
| 13 |
Lose_between_2500_5000 |
Trump |
0.49 |
| 12 |
Lose_between_1000_2500 |
Other |
0.08 |
| 11 |
Lose_between_1000_2500 |
Clinton |
0.46 |
| 10 |
Lose_between_1000_2500 |
Trump |
0.46 |
| 9 |
Gain_1000_or_Less |
Other |
0.09 |
| 8 |
Gain_1000_or_Less |
Clinton |
0.50 |
| 7 |
Gain_1000_or_Less |
Trump |
0.41 |
| 6 |
Gain_1000.2500 |
Other |
0.08 |
| 5 |
Gain_1000.2500 |
Clinton |
0.49 |
| 4 |
Gain_1000.2500 |
Trump |
0.43 |
| 3 |
Gain_2500_or_Greater |
Other |
0.07 |
| 2 |
Gain_2500_or_Greater |
Clinton |
0.47 |
| 1 |
Gain_2500_or_Greater |
Trump |
0.46 |
black.bold.italic.12.text <- element_text(face = "bold.italic", color = "black", size = 8)
votePlot <- ggplot(new) + geom_bar(aes(y = new$Percent.Of.The.Time, x = new$If.You.Stand.To, fill = new$You.Voted.For), stat = "identity") +
scale_fill_hue(l=40) +
scale_fill_brewer(palette="Spectral") + coord_flip()+ theme(axis.text = black.bold.italic.12.text) + guides(fill = guide_legend(reverse=TRUE)) +
xlab("Amount You Will Lose") +
ylab("Percentage of Voters")
## Scale for 'fill' is already present. Adding another scale for 'fill',
## which will replace the existing scale.
votePlot

ggplot(new, aes(x = If.You.Stand.To, y = Percent.Of.The.Time)) +
geom_bar(aes(fill = You.Voted.For), stat = "identity", position = position_dodge()) +
scale_fill_hue(l=40) +
#scale_fill_brewer(palette="Spectral") +
coord_flip() + theme(axis.text = black.bold.italic.12.text) +
#reverses legend order
guides(fill = guide_legend(reverse=TRUE)) + xlab("Amount You Will Lose")+ylab("Percentage of Voters")

ggplot(new, aes(x = If.You.Stand.To, y = Percent.Of.The.Time)) +
geom_line(aes(colour = You.Voted.For, group = You.Voted.For)) +
geom_point(aes(colour = You.Voted.For), size = 3) +
theme(axis.text = black.bold.italic.12.text) + scale_fill_hue(l=40) +
guides(fill = guide_legend(reverse=TRUE)) + xlab("Amount You Will Lose") +
ylab("Percentage of Voters") +
scale_fill_manual(values=c("#CC6666", "#9999CC", "#66CC99"))

## [1] 21 3
| Trump |
-7500 |
0.58 |
| Clinton |
-7500 |
0.39 |
| Other |
-7500 |
0.03 |
| Trump |
-5000 |
0.60 |
| Clinton |
-5000 |
0.35 |
| Other |
-5000 |
0.05 |
| Trump |
-2500 |
0.49 |
| Clinton |
-2500 |
0.45 |
| Other |
-2500 |
0.06 |
| Trump |
-1000 |
0.46 |
| Clinton |
-1000 |
0.46 |
| Other |
-1000 |
0.08 |
| Trump |
1000 |
0.41 |
| Clinton |
1000 |
0.50 |
| Other |
1000 |
0.09 |
| Trump |
2500 |
0.43 |
| Clinton |
2500 |
0.49 |
| Other |
2500 |
0.08 |
| Trump |
5000 |
0.46 |
| Clinton |
5000 |
0.47 |
| Other |
5000 |
0.07 |