Introduction

This is a follow up tutorial of the dplyr elections tutorial. In this document we are going to learn the basics of ggplot2, one of the best data visualization packages of R.

Where is the data?

For starters, here is the data.

library(tidyverse) #tidyverse includes both dplyr and ggplot2
raw_df <- readRDS("local_election_20190623_education.rds")

Fundamental Plots

Different from dplyr we do not use the pipe (%>%), but the plus sign (+). Actually we do not chain commands in ggplot2, but we start with a “canvas” and add layers. The canvas is called a ggplot2() function Then for x and y axes, we define an “(aes)thetic” with the aes() function inside ggplot2.

Scatter Plot

Scatter plot or dot plot is represented by the geom_point(). For instance let’s plot votes of AK PARTI and CHP.

We start with preparing our data. We need a data in the long format (hence, pivot_longer function).

plot_df <- raw_df %>% filter(ilce_ADI == "ADALAR") %>% group_by(muhtarlik_ID,muhtarlik_ADI) %>% summarise(AK_PARTI = sum(AK_PARTI), CHP = sum(CHP)) %>% ungroup() %>% pivot_longer(cols=CHP:AK_PARTI,names_to="party",values_to="votes") 
print(plot_df,n=3)
## # A tibble: 10 x 4
##   muhtarlik_ID muhtarlik_ADI  party    votes
##   <chr>        <chr>          <chr>    <dbl>
## 1 95626        BURGAZADA MAH. CHP        874
## 2 95626        BURGAZADA MAH. AK_PARTI   177
## 3 95627        MADEN MAH.     CHP       2070
## # … with 7 more rows
scplot <- ggplot(plot_df,aes(x=muhtarlik_ADI,y=votes,color=party)) + geom_point()
scplot

Congratulations! You did your first plot. Other plots have very similar attributes. But before that, let’s make our plot more beautiful. First let’s remove all the gray background with a predefined “theme” (there are very interesting themes in ggplot2).

scplot + theme_minimal()

Then let’s change label names, add a title, rotate x axis text.

scplot + 
  theme_minimal() + 
  labs(x="Neighborhood Name",y="# of Votes",title="Votes of AK PARTI and CHP at ADALAR district",color="Party") + 
  theme(axis.text.x = element_text(angle=90),legend.position = "top")

Bar Chart

Bar chart is very similar to geom_point, now we use geom_bar. And instead of color we use fill.

ggplot(plot_df,aes(x=muhtarlik_ADI,y=votes, fill=party)) + geom_bar(stat="identity")

But it is not quite what we wanted. It would be better if it is side by side.

ggplot(plot_df,aes(x=muhtarlik_ADI,y=votes, fill=party)) + geom_bar(stat="identity",position="dodge") + 
  theme_minimal() + 
  labs(x="Neighborhood Name",y="# of Votes",title="Votes of AK PARTI and CHP at ADALAR district",fill="Party") + 
  theme(axis.text.x = element_text(angle=90),legend.position = "top") +
  scale_fill_manual(values=c("#FFA351FF","#E10600FF"))

References