Tuesday Lecture: playing with the gapminder data

Explore other relationships in the gapminder data using what you learned today, could be considering other variables in the data set, or using an alternative geometry or faceting with other variables. Just make one figure, but using effectively as many of the concepts you learned as possible.

This data visualization cheat sheet might be helpful

Here is the data

data(gapminder)

Thursday Lecture

moviesdatapath <- "https://raw.githubusercontent.com/mine-cetinkaya-rundel/df2016_workshops/master/viz_ggplot2_shiny/data/movies.csv"
movies = readr::read_csv(url(moviesdatapath))
## `geom_smooth()` using formula 'y ~ x'

Thursday Lecture: pipes

Using the data.frame below, we want to:

library(magrittr)
X <- data.frame(x=sample(c("q","l","m"),50,replace=T),
                y=sample(c("a","b","c"),50,replace=T),
                z=sample(c("c","d","e"),50,replace=T))
str(X)
## 'data.frame':    50 obs. of  3 variables:
##  $ x: chr  "l" "q" "q" "m" ...
##  $ y: chr  "b" "b" "c" "a" ...
##  $ z: chr  "d" "c" "c" "c" ...

Let’s do this by:

  • Taking one intermediate step at a time save to same object
tabs.xy <- X[1:2]
(tabs.xy <- table(tabs.xy))
##    y
## x    a  b  c
##   l  4  6  2
##   m  6  9  2
##   q  5 10  6
count.y <- X[2]
(count.y <- table(count.y))
## count.y
##  a  b  c 
## 15 25 10
mode.x <- X[1]
mode.x <- table(mode.x)
mode.x <- mode.x == max(mode.x)
(mode.x <- names(mode.x)[mode.x])
## [1] "q"
  • Using composite functions
(tabs.xy <- table(X[1:2]))
##    y
## x    a  b  c
##   l  4  6  2
##   m  6  9  2
##   q  5 10  6
(count.y <- table(X[2]))
## 
##  a  b  c 
## 15 25 10
(count.x <- names(table(X[1]))[table(X[1])==max(table(X[1]))])
## [1] "q"
  • Using pipes
X %>% .[1:2] %>% table()
##    y
## x    a  b  c
##   l  4  6  2
##   m  6  9  2
##   q  5 10  6
X %>% .[1] %>% table() -> cheating1
cheating1 %>% names() %>% .[cheating1==max(cheating1)]
## [1] "q"