DA 101, Dr. Ladd
Week 4
Look at the documentation for the mpg
data set. What visualization type would you use to compare the counts of each type of car? Which variables would you use, and what kind of variables are they? Jot down your answers.
Look at the mpg
documentation again. What visualization type would you use to compare the distribution of city fuel efficiency among different drive trains? Which variables would you use, and what kind of variables are they? Jot down your answers.
More on these viz types in future lessons!
ggplot2
tidyverse
or
ggplot()
function takes two arguments, data and a mappingYou can use this with or without the argument names data =
and mapping =
.
ggplot(data = YourDataFrame, mapping = aes(x = FirstVariable, y = SecondVariable, color = ThirdVariable))
In this example, FirstVariable
becomes the x-axis and SecondVariable
becomes the y-axis. You can also add a mapping for color
.
+
sign.ggplot(data = YourDataFrame, mapping = aes(x = FirstVariable, y = SecondVariable)) +
geom_point() +
geom_smooth() +
facet_wrap(~ThirdVariable)
The +
works sort of like the %>%
in dplyr
.
geom
layers.geom_point()
geom_bar()
geom_boxplot()
geom_histogram()
You can put aesthetic mappings inside geom layers if you prefer!
Create a plot to compare the price of a diamond to its weight using ggplot
’s built-in diamonds dataset. Then create the same plot but show color as the quality of the cut.
facet
layers.facet_wrap(~x)
: Use a single categorical variable.
facet_grid(x ~ y)
: Use two categorical variables.
stat
layers.stat_count()
counts up categories and works behind the scenes in geom_bar()
.stat_bin()
creates countable segments of continuous data and works behind the scenes in geom_histogram()
.stat_summary()
lets you summarize any aspect of your data.?
to get more information about any ggplot layer.Create a plot showing the distribution of weight in the diamonds dataset. Then show the distributions according to each color of diamond. Finally, change the size of the bins in each distribution to show more detail.