geom_point

# data()
# ?mpg
#mpg
ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point()

color!

ggplot(data = mpg, aes(x = displ, y = hwy, color=class)) + 
  geom_point()

shape: need a b&w figure

Changed color to shape:

ggplot(data = mpg, aes(x = displ, y = hwy, shape=class)) + 
  geom_point()
## Warning: The shape palette can deal with a maximum of 6 discrete values
## because more than 6 becomes difficult to discriminate; you have 7.
## Consider specifying shapes manually if you must have them.
## Warning: Removed 62 rows containing missing values (geom_point).

size

#?mpg
ggplot(data = mpg, aes(x = cty, y = hwy, size = class, color = fl)) + 
  geom_point(alpha = 0.2)
## Warning: Using size for a discrete variable is not advised.

#?mpg
ggplot(data = mpg, aes(x = cty, y = hwy, size = class)) + 
  geom_point(alpha = 0.2, color = "blue")
## Warning: Using size for a discrete variable is not advised.

Exercise

ggplot(data = mpg) + 
      geom_point(aes(x = displ, y = hwy), color = "blue")

ggplot(data = mpg) + 
      geom_point(aes(x = displ, y = hwy, color = displ < 5))

Geoms

ggplot(mpg, aes(x = drv, y = hwy)) +
  geom_violin()

Multiple geoms

ggplot(data = mpg) +
  geom_point(aes(x = displ, y = hwy)) +
  geom_smooth(aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

?geom_smooth

ggplot(data = mpg, aes(x = displ, y = hwy))+
  geom_point() +
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Customization

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth() +
  labs(title = "Relationship b/w engine size & MPG",
       x = "Highway MPG",
       y = "Engine displacement (liters)") +
  theme_bw() +
  theme(text = element_text(size = 16))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

Boxplot

dplyr

gapminder <- readr::read_csv('https://raw.githubusercontent.com/jules32/2017-11-30-MBARI/gh-pages/data/gapminder.csv') 
## Parsed with column specification:
## cols(
##   country = col_character(),
##   year = col_integer(),
##   pop = col_double(),
##   continent = col_character(),
##   lifeExp = col_double(),
##   gdpPercap = col_double()
## )
y <- gapminder %>%
  filter(country == "Sweden") %>%
  summarize(
    mean_lifeExp = mean(lifeExp))

y
## # A tibble: 1 x 1
##   mean_lifeExp
##          <dbl>
## 1       76.177
gapminder %>%
  filter(country == c("Egypt", "Vietnam"))
## # A tibble: 12 x 6
##    country  year      pop continent lifeExp gdpPercap
##      <chr> <int>    <dbl>     <chr>   <dbl>     <dbl>
##  1   Egypt  1952 22223309    Africa  41.893 1418.8224
##  2   Egypt  1962 28173309    Africa  46.992 1693.3359
##  3   Egypt  1972 34807417    Africa  51.137 2024.0081
##  4   Egypt  1982 45681811    Africa  56.006 3503.7296
##  5   Egypt  1992 59402198    Africa  63.674 3794.7552
##  6   Egypt  2002 73312559    Africa  69.806 4754.6044
##  7 Vietnam  1957 28998543      Asia  42.887  676.2854
##  8 Vietnam  1967 39463910      Asia  47.838  637.1233
##  9 Vietnam  1977 50533506      Asia  55.764  713.5371
## 10 Vietnam  1987 62826491      Asia  62.820  820.7994
## 11 Vietnam  1997 76048996      Asia  70.672 1385.8968
## 12 Vietnam  2007 85262356      Asia  74.249 2441.5764
gapminder %>%
  filter(country %in% c("Egypt", "Vietnam"))
## # A tibble: 24 x 6
##    country  year      pop continent lifeExp gdpPercap
##      <chr> <int>    <dbl>     <chr>   <dbl>     <dbl>
##  1   Egypt  1952 22223309    Africa  41.893  1418.822
##  2   Egypt  1957 25009741    Africa  44.444  1458.915
##  3   Egypt  1962 28173309    Africa  46.992  1693.336
##  4   Egypt  1967 31681188    Africa  49.293  1814.881
##  5   Egypt  1972 34807417    Africa  51.137  2024.008
##  6   Egypt  1977 38783863    Africa  53.319  2785.494
##  7   Egypt  1982 45681811    Africa  56.006  3503.730
##  8   Egypt  1987 52799062    Africa  59.797  3885.461
##  9   Egypt  1992 59402198    Africa  63.674  3794.755
## 10   Egypt  1997 66134291    Africa  67.217  4173.182
## # ... with 14 more rows
# find max gdpPercap of Egypt and Vienam
gapminder %>%
  group_by(country) %>%
  filter(
    country %in% c("Egypt", "Vietnam"),
    gdpPercap == max(gdpPercap))
## # A tibble: 2 x 6
## # Groups:   country [2]
##   country  year      pop continent lifeExp gdpPercap
##     <chr> <int>    <dbl>     <chr>   <dbl>     <dbl>
## 1   Egypt  2007 80264543    Africa  71.338  5581.181
## 2 Vietnam  2007 85262356      Asia  74.249  2441.576