You can take an entire class on data visualization. Sadly, the guiding principles and theory of data visualization is beyond the scope of our current class.
So what will you learn in this section?
ggplot2
& some new thingsPlease make sure the following packages are installed:
tidyverse
(this includes dplyr
and ggplot2
)RColorBrewer
ggpubr
wesanderson
packageRemember: do NOT memorize anything! You will always have these slides to refer back to, and Google is your friend. Looking things up is a good thing!
ggplot2
ggplot2
gg
in ggplot2
stands for "the grammar of graphics"ggplot2
has the following structure:ggplot(things that impact the entire plot) + geom_something(things that impact just the something)
ggplot2
ggplot2
has the following structure:
ggplot(things that impact the entire plot) + geom_something(things that impact just the something)
Things like:
ggplot
What happens if you only input your data and what you want your x- and y-axes to be?
ggplot(data = empire, aes(x = mass, y = height))
geom_
ggplot2
has the following structure:
ggplot(things that impact the entire plot) + geom_something(things that impact just the something)
geom_
typically means shape. What shapes do you want to use to represent your data in the plot?
geom_histogram
-- histogramgeom_density
-- distributionsgeom_violin
-- distributionsgeom_point
-- scatter plotgeom_col
-- bar plotThese are functions that are searchable in the help pages!
ggplot2
The functions ggplot()
and geom_()
can take on different aesthetics as an argument, using aes()
.
Aesthetics are how you control what you want your plot to look like; how can you make it pretty? Examples:
x-
and y-
axes?color
(should you color the plot by some variable?)fill
(very similar to color
, should you fill the plot in somehow; used for bar graphs and boxplots)shape
(do you want groups to have different shaped points?)size
(how big should plotted data be?)Let's say you want to change something about your plot...
aes()
aes()
ggplot(data = empire, aes(x = mass, y = height)) + geom_point()
Let's say you want to change something about your plot...
aes()
aes()
ggplot(data = empire, aes(x = mass, y = height)) + geom_point(color = "cornflowerblue")
Let's say you want to change something about your plot...
aes()
aes()
ggplot(data = empire, aes(x = mass, y = height)) + geom_point(aes(color = species))
ggplot(data = empire, aes(x = mass, y = height)) + geom_point(color = "purple", aes(shape = species), size = 3)
labs
labs()
functionggplot(data = empire, aes(x = mass, y = height)) + geom_point(aes(color = species, shape = species)) + labs(x = "Weight in kg", y = "Height in cm", title = "Size of Star Wars Characters")
alpha =
is an argument that controls transparency (1 = opaque, 0 = transparent)alpha
parameter...ggplot(data = empire, aes(x = height)) + geom_density(aes(fill = sex)) + labs(x = "Height in cm", y = "Probability Density", title = "Height Distributions")
alpha =
is an argument that controls transparency (1 = opaque, 0 = transparent)alpha
parameter...ggplot(data = empire, aes(x = height)) + geom_density(aes(fill = sex), alpha = .5) + labs(x = "Height in cm", y = "Probability Density", title = "Height Distributions")
geom_bar()
uses count
as default for the y-axistable()
table(empire$species)
## ## Droid Human Wookiee Yoda's species ## 2 6 1 1
ggplot(data = empire, aes(x = species)) + geom_bar(aes(fill = species))
Let's say we want the order (from left to right) to be Human, Yoda's species, Wookiee, Droid.
species
factor like so:empire$species <- factor(empire$species, levels = c("Human", "Yoda's species", "Wookiee", "Droid"))
Let's say we want the order (from left to right) to be Human, Yoda's species, Wookiee, Droid.
species
factor like so:empire$species <- factor(empire$species, levels = c("Human", "Yoda's species", "Wookiee", "Droid"))
Now, if we simply go the tables of counts again, it should match our new ordering:
table(empire$species)
## ## Human Yoda's species Wookiee Droid ## 6 1 1 2
ggplot(data = empire, aes(x = species)) + geom_bar(aes(fill = species))
You can take an entire class on data visualization. Sadly, the guiding principles and theory of data visualization is beyond the scope of our current class.
So what will you learn in this section?
ggplot2
& some new thingsKeyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |