data visualization

data

mapping (aesthetics)

Geometric representation

statistics

facet

coordinate space

labels

theme

install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.0     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ggplot(data = BOD,
       mapping = aes(x = Time, 
                     y = demand)) + # creates the graph but does not have points 
  geom_point(size = 5) + # geom adds points, editing point size 
  geom_line(color = "red") #adding a line and changning color

Keeping it simple

ggplot(BOD, aes(Time, demand))+
  geom_point(size = 3)+
  geom_line(color = "red")

New Data

CO2 %>%
  ggplot(aes(conc, uptake,
             color = Treatment))+
  geom_point(size = 3, alpha = 0.5)+
  geom_smooth(method = lm, se = F)+ 
  facet_wrap (~Type) +
  labs (title = "Concentration of CO2")+
  theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

Doing CO2 Again

CO2 %>%
  ggplot(aes(Treatment, uptake))+ 
  geom_boxplot()+
  geom_point(alpha = 0.5,
             aes(size= conc,
                 color = Plant)) +
  facet_wrap(~Type)+
  coord_flip()+
  theme_bw()+ 
  labs(title = "Chilled vs Non-chilled")

(alpha) is how transparent something is

MPG Data

head(mpg)
## # A tibble: 6 × 11
##   manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class 
##   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr> 
## 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compa…
## 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compa…
## 3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compa…
## 4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compa…
## 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compa…
## 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p     compa…
mpg %>%
  filter(cty<25) %>%
  ggplot(aes(displ, cty))+
  geom_point(aes(color = drv,
                 size = trans),
             alpha = 0.5) + 
  geom_smooth(method = lm) + 
  facet_wrap(~year, nrow = 1)+
  labs(x = "Engine size",
       y = "MPG in the city",
       title = "Fuel efficiency")+ 
  theme_bw()
## Warning: Using size for a discrete variable is not advised.
## `geom_smooth()` using formula = 'y ~ x'