2023-01-24 ggplot2

Abigail Griffin

2023-01-26

ggplot2

January 24 2023
AGG

Load libraries.

library(ggplot2)
library(ggthemes)
library(patchwork)

Template for ggplot code

# p1<- ggplot(data=<DATA>, mapping=aes(x=xVar, y=yVar)) +
# <GEOM FUNCTION> + # geom_boxplot()
# if you wanted to map on a boxplot with your data
# print(p1)

Load in a built-in data set

d<-mpg
str(mpg)
## tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
##  $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
##  $ model       : chr [1:234] "a4" "a4" "a4" "a4" ...
##  $ displ       : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
##  $ year        : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
##  $ cyl         : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
##  $ trans       : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
##  $ drv         : chr [1:234] "f" "f" "f" "f" ...
##  $ cty         : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
##  $ hwy         : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
##  $ fl          : chr [1:234] "p" "p" "p" "p" ...
##  $ class       : chr [1:234] "compact" "compact" "compact" "compact" ...
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
glimpse(d)
## Rows: 234
## Columns: 11
## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
## $ model        <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
## $ displ        <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
## $ year         <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
## $ cyl          <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
## $ trans        <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
## $ drv          <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
## $ cty          <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
## $ hwy          <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
## $ fl           <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
## $ class        <chr> "compact", "compact", "compact", "compact", "compact", "c…
  • How to do qplot() (quick plots): for quick plotting, similar to what is in R base package.
    • e.g. make a histogram
qplot(x=d$hwy)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

qplot(x=d$hwy, fill=I("darkblue"), color=I("black"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# need to put I before color 
  • Make a scatter plot
library(dplyr)
library(ggplot2)
qplot(x=d$displ, y=d$hwy, geom=c("smooth", "point"), method="lm") # method makes it a linear distribution and not just following points
  • Make it a box plot
qplot(x=d$fl, y=d$cty, geom="boxplot", fill=I("forestgreen"))
  • Make a basic bar plot
qplot(x=d$fl, geom="bar", fill=I("forestgreen"))
# don't need y axis for bar plot so skip it

Create some data with specified counts

x_trt<-c("Control", "Low", "High")
y_resp<-c(12, 2.5, 22.9)
qplot(x=x_trt, y=y_resp, geom="col", fill=I(c("forestgreen", "slategray", "goldenrod"))) # will print in alphabetical order
# uses vectors instead of whole data frame

ggplot: uses dataframes instead of vectors

p1<-ggplot(data=d, mapping=aes(x=displ, y=cty, color=cyl)) +
  geom_point()
p1

# aes is very important: construct aesthetics mappings
p1 + theme_base() # changes to what it was when it was base R

p1 + theme_bw()

p1 + theme_classic() # removes background

p1 + theme_linedraw()

p1 + theme_dark() # adds dark background

p1 + theme_minimal() # no axis lines

p1 + theme_economist()

# google ggplot themes to find more
p1 + theme_bw(base_size=30, base_family="serif")

p2<-ggplot(data=d, aes(x=fl, fill=fl)) +
  geom_bar()
p2

p2 + coord_flip() #flips x and y axes

p2 + coord_flip() + theme_classic(base_size=15, base_family="sans")

### More theme modifications

p3<-ggplot(data=d, aes(x=displ, y=cty)) +
  geom_point(size=7, shape=21, color="magenta", fill="black") +
  xlab("Count") + # add x axis label
  ylab("Fuel") +
  labs(title="My Title Here", subtitle="My subtitle goes here") # can also do x=/y= for axis
p3

p3 + xlim(1,10) + ylim(0,35) # x is between 1 and 10, y is between 0 and 35

* Make another boxplot

library(viridis)
## Loading required package: viridisLite
cols<-viridis(7, option = "magma") # takes 7 hex color codes from the viridis color packages. # also plasma, turbo, viridis. # have to give it the same number of colors as you have groups
ggplot(data=d, aes(x=class,y=hwy, fill=class)) +
  geom_boxplot() +
  scale_fill_manual(values=cols)

Stacking graphs

library(patchwork)
p1+p2+p3 # puts them all next to each other
p1/p2/p3 # puts them on top of each other
(p1+p2)/p3 # p1 and p2 on top over p3