-
Notifications
You must be signed in to change notification settings - Fork 2
/
ggplot_example.R
35 lines (29 loc) · 1.09 KB
/
ggplot_example.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
source('styleguide.R')
house = read.csv("house_elections.csv")
# CLEANING
party.temp = as.character(house$party)
party.temp[house$party != "democrat" & house$party != "republican"] = "other"
party.temp[house$writein] = "writein"
house$party = factor(party.temp, levels = c("democrat", 'republican', 'other', 'writein'))
table(house$party)
mi = house[house$electiondistrict == "MI3",]
mi = mi[mi$party != 'writein',]
mi = mi[mi$party != 'other',]
# Plotting
a <- ggplot(data=mi, aes(x=year, y=candidatevotes)) +
geom_line(aes(colour = factor(party))) +
geom_point(aes(colour = factor(party))) +
scale_color_manual(values = c("#0015bc", "#E9141D"),name="Party", labels = c('Democrat', 'Republican')) +
labs(title="MI-3 Elections") +
xlab("Year") +
ylab("Votes") +
ylim(c(0,225000)) +
xlim(c(1975,2019)) +
theme(legend.position="bottom") +
labs(subtitle = "1976 - 2018", caption = "Source: https://electionlab.mit.edu/data") +
theme_hodp()
a
# Add logo
grid::grid.raster(logo, x = 0.01, y = 0.01, just = c('left', 'bottom'), width = unit(2, 'cm'))
# Interactive Graphic
ggplotly(a)