Tour the Cheatsheet

Harold Nelson

6/12/2017

Load Libraries

library(tidyverse)
## ── Attaching packages ───────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0     ✔ purrr   0.2.5
## ✔ tibble  1.4.2     ✔ dplyr   0.7.8
## ✔ tidyr   0.8.2     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ──────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(RColorBrewer)
library(ggplot2movies)

Create Base Objects

a <- ggplot(mpg, aes(hwy))
b <- ggplot(mpg, aes(fl))
#c <- ggplot(map, aes(long, lat))
#d <- ggplot(economics,aes(date,unemploy))
#e <- ggplot(seals,aes(x=long,y=lat))
f <- ggplot(mpg,aes(cty,hwy))
g <- ggplot(mpg,aes(class,hwy))
h <- ggplot(diamonds,aes(cut,color))
#i <- ggplot(movies,aes(year,rating))
#j <- ggplot(economics,aes(date,unemploy))

#df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
#k <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))

dat1 <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests)))
#map <- map_data("state")
l <- ggplot(dat1, aes(fill = murder))

seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
m <- ggplot(seals, aes(long, lat))


r <- b + geom_bar()
s <- ggplot(mpg, aes(fl, fill = drv))
t <- ggplot(mpg, aes(cty, hwy)) + geom_point()

Describing One Continuous Variable

Use “a” above to start. Use geom_density() with the default settings.

Answer

a + geom_density()

Exercise

Now try changing the parameter “adjust.”

Answer

a + geom_density(adjust = .5) + ggtitle("adjust = .5")

a + geom_density(adjust = .25 ) + ggtitle("adjust = .25")

Exercise

Do a basic histogram

Answer

a + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Exercise

Try to vary the number of bins from the default 30.

Answer

a + geom_histogram(bins=10) + ggtitle("bins = 10")

a + geom_histogram(bins=45) + ggtitle("bins = 45")

Exercise

Try a violin plot

Answer

a + geom_violin(aes(y=hwy))

Exercise

Try playing with adjust for more detail.

Answer

a + geom_violin(aes(y=hwy),adjust = .5) + ggtitle("adjust = .5")

a + geom_violin(aes(y=hwy),adjust = .25) + ggtitle("adjust = .25")