Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 36 additions & 22 deletions exercise-1/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,83 @@

# Install and load `ggplot2`
# You will also want to load `dplyr`


install.packages("ggplot2")
library("ggplot2")
library("dplyr")
# For this exercise you'll be working with the `diamonds` data set included in the ggplot2 library
# Use `?diamonds` to get more information about this data set (including the column descriptions
# Also check the _column names_ and the _number of rows_ in the data set


View(diamonds)
colnames(diamonds)
nrow(diamonds)
# This data set has a lot of rows. To make things a bit more readable,
# use dplyr's `sample_n()` function to get a random 1000 rows from the data set
# Store this sample in a variable `diamonds.sample`

diamonds.sample <- sample_n(diamonds, 1000)

# Start by making a new `ggplot` with the `diamonds.sample` as the data (no geometry yet)
# What do you see?

ggplot(data = diamonds.sample)

# Draw a scatter plot (with point geometry) with for the `diamonds.sample` set,
# with the `carat` mapped to the x-position and `price` mapped to the y-position.


ggplot(data = diamonds.sample) +
geom_point(mapping = aes(x = carat, y = price))
View(diamonds.sample)
# Draw the same plot as above, but color each of the points based on their clarity.

ggplot(data = diamonds.sample) +
geom_point(mapping = aes(x = carat, y = price, color = clarity))

# Draw the same plot as above, but for the entire `diamonds` data set. Note this may take
# a few seconds to generate.

ggplot(data = diamonds) +
geom_point(mapping = aes(x = carat, y = price, color = clarity))

# Draw another scatter plot for `diamonds.sample` of price (y) by carat (x),
# but with all of the dots colored "blue".
# Hint: you'll need to set the color channel, not map a value to it!

ggplot(data = diamonds.sample) +
geom_point(mapping = aes(x = carat, y = price, color = clarity), color = "blue")

# Draw a scatter plot for `diamonds.sample` of `price` by `carat`, where each
# point has an aesthetic _shape_ based on the diamond's `cut`.


ggplot(data = diamonds.sample) +
geom_point(mapping = aes(x = carat, y = price, shape = cut))
?aes
# Draw a scatter plot for `diamonds.sample` of *`cut`* by `carat`, where each
# point has an aesthetic _size_ based on the diamond's *`price`*

ggplot(data = diamonds.sample) +
geom_point(mapping = aes(x = cut, y = carat, size = price))

# Try coloring the above plot based on the diamond's price!

ggplot(data = diamonds.sample) +
geom_point(mapping = aes(x = cut, y = carat, size = price, color = price))

# Draw a line plot (with line geometry) for `diamonds.sample`. The x-position should be mapped to
# carat, y-position to price, and color to carat.

ggplot(data = diamonds.sample) +
geom_line(mapping = aes(x = carat, y = price, color = carat))

# That's kind of messy. Try using `smooth` geometry instead.

ggplot(data = diamonds.sample) +
geom_smooth(mapping = aes(x = carat, y = price, color = carat))

# Draw a plot with bar geometry (a bar chart), mapping the diamond's `cut` to the x-axis


ggplot(data = diamonds.sample) +
geom_bar(mapping = aes(x = cut))
# Add an aesthetic property that will _fill_ each bar geometry based on the `clarity` of the diamonds
# What kind of chart do you get?

ggplot(data = diamonds.sample) +
geom_bar(mapping = aes(x = cut, fill = clarity))

# Draw a histogram of diamond prices.
# Try mapping each bar based on clarity as well!

ggplot(data = diamonds.sample) +
geom_histogram(mapping = aes(x = price, fill = clarity))

# (For a more traditional "bell-curve", make a histogram of diamond `depths`)

ggplot(data = diamonds.sample) +
geom_histogram(mapping = aes(x = depth, fill = clarity))
# Draw a plot of the `diamonds.sample` data (price by carat), with both points for each
# diamond AND smoothed lines for each cut (hint: in a separate color)
# Making the points have some `alpha` transparency will make the plot look nicer
Expand Down
10 changes: 6 additions & 4 deletions exercise-2/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@

# Install and load `ggplot2`
# install.packages("ggplot2") # if needed

library(ggplot2)

# For this exercise you will again be working with the `diamonds` data set.
# Use `?diamonds` to review details about this data set



## Statistical Transformations

View(diamonds)
# Draw a bar chart of the diamonds data, organized by cut
# The height of each bar is based on the "count" (number) of diamonds with that cut

ggplot(data = diamonds, aes(x = cut)) +
geom_bar()

# Use the `stat_count` to apply the statistical transformation "count" to the diamonds
# by cut. You do not need a separate geometry layer!

ggplot(data = diamonds, aes(x = cut)) +
stat_count()

# Use the `stat_summary` function to draw a chart with a summary layer.
# Map the x-position to diamond `cut`, and the y-position to diamond `depth`
Expand Down