0% found this document useful (0 votes)
44 views

Objects and Layers

The document discusses plotting and summarizing data from the mtcars dataset in R. It demonstrates how to plot variables from mtcars and add linear regression lines. It also shows converting variables to factors and using ggplot2 to create scatter plots, adjusting aesthetics like color, shape, size. Finally, it demonstrates using tidyr to reshape the iris dataset and plot different variables with facets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Objects and Layers

The document discusses plotting and summarizing data from the mtcars dataset in R. It demonstrates how to plot variables from mtcars and add linear regression lines. It also shows converting variables to factors and using ggplot2 to create scatter plots, adjusting aesthetics like color, shape, size. Finally, it demonstrates using tidyr to reshape the iris dataset and plot different variables with facets.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Objects and layers

# Plot the correct variables of mtcars


plot(mtcars$wt, mtcars$mpg, col = mtcars$cyl)

# Change cyl inside mtcars to a factor


mtcars$fcyl <- as.factor(mtcars$cyl)

# Make the same plot as in the first instruction


plot(mtcars$wt, mtcars$mpg, col = mtcars$fcyl)

# Use lm() to calculate a linear model and save it as carModel


carModel <- lm(mtcars$mpg~mtcars$wt, data = mtcars)

# Basic plot
mtcars$cyl <- as.factor(mtcars$cyl)
plot(mtcars$wt, mtcars$mpg, col = mtcars$cyl)

# Call abline() with carModel as first argument and set lty to 2


abline(carModel, lty = 2)

# Plot each subset efficiently with lapply


# You don't have to edit this code
plot(mtcars$wt, mtcars$mpg, col = mtcars$cyl)
lapply(mtcars$cyl, function(x) {
abline(lm(mpg ~ wt, mtcars, subset = (cyl == x)), col = x)
})

# This code will draw the legend of the plot


# You don't have to edit this code
legend(x = 5, y = 33, legend = levels(mtcars$cyl),
col = 1:3, pch = 1, bty = "n")

# Convert cyl to factor (don't need to change)


mtcars$cyl <- as.factor(mtcars$cyl)

# Example from base R (don't need to change)


plot(mtcars$wt, mtcars$mpg, col = mtcars$cyl)
abline(lm(mpg ~ wt, data = mtcars), lty = 2)
lapply(mtcars$cyl, function(x) {
abline(lm(mpg ~ wt, mtcars, subset = (cyl == x)), col = x)
})
legend(x = 5, y = 33, legend = levels(mtcars$cyl),
col = 1:3, pch = 1, bty = "n")

# Plot 1: add geom_point() to this command to create a scatter


plot
ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point()

# Plot 2: include the lines of the linear models, per cyl


ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE)

# Plot 3: include a lm for the entire dataset in its whole


ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
geom_smooth(aes(group = 1), method = "lm", se = FALSE,
linetype = 2)
# Consider the structure of iris, iris.wide and iris.tidy (in
that order)
str(iris)
str(iris.wide)
str(iris.tidy)
# Think about which dataset you would use to get the plot shown
right
# Fill in the ___ to produce the plot given to the right
ggplot(iris.tidy, aes(x = Species, y = Value, col = Part)) +
geom_jitter() +
facet_grid(. ~ Measure)
# Load the tidyr package
library(tidyr)

# Fill in the ___ to produce to the correct iris.tidy dataset


iris.tidy <- iris %>%
gather(key, Value, -Species) %>%
separate(key, c("Part", "Measure"), "\\.")
head(iris)
en esta opcion la variable Species que contiene valores como
Sepal.Width, Sepal.Length y Petal.Legth y Petal.Width, toman
todos los valores de Value.
# Consider the head of iris, iris.wide and iris.tidy (in that
order)
head(iris)
head(iris.wide)
head(iris.tidy)

# Think about which dataset you would use to get the plot shown
right
# Fill in the ___ to produce the plot given to the right
ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) +
geom_jitter() +
facet_grid(. ~ Species)

# Add column with unique ids (don't need to change)


iris$Flower <- 1:nrow(iris)
iris$Flower
# Fill in the ___ to produce to the correct iris.wide dataset
iris.wide <- iris %>%
gather(key, value, -Species, -Flower) %>%
separate(key, c("Part","Measure"), "\\.") %>%
iris.wide
# 1 - Map mpg to x and cyl to y
ggplot(mtcars, aes(x = mpg, y = cyl)) +
geom_point()

# 2 - Reverse: Map cyl to x and mpg to y


ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_point()

# 3 - Map wt to x, mpg to y and cyl to col


ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point()

# Change shape and size of the points in the above plot


ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point(shape = 1, size = 4)
# am and cyl are factors, wt is numeric
class(mtcars$am)
class(mtcars$cyl)
class(mtcars$wt)

# Given from the previous exercise


ggplot(mtcars, aes(x = wt, y = mpg, col = cyl)) +
geom_point(shape = 1, size = 4)

# 1 - Map cyl to fill


ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(shape = 1, size = 4)
# 2 - Change shape and alpha of the points in the above plot
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(shape = 21, size = 4, alpha = 0.6)

# 3 - Map am to col in the above plot


ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl, col = am)) +
geom_point(shape = 21, size = 4, alpha = 0.6)

# Map cyl to size


ggplot(mtcars, aes(x = wt, y = mpg, size = cyl)) +
geom_point()
# Map cyl to alpha
ggplot(mtcars, aes(x = wt, y = mpg, alpha = cyl)) +
geom_point()

# Map cyl to shape


ggplot(mtcars, aes(x = wt, y = mpg, shape = cyl)) +
geom_point()
# Map cyl to labels
ggplot(mtcars, aes(x = wt, y = mpg, label = cyl)) +
geom_text()

# Map cyl to size


ggplot(mtcars, aes(x = wt, y = mpg, size = cyl)) +
geom_point()

# Map cyl to alpha


ggplot(mtcars, aes(x = wt, y = mpg, alpha = cyl)) +
geom_point()

# Map cyl to shape


ggplot(mtcars, aes(x = wt, y = mpg, shape = cyl)) +
geom_point()
# Map cyl to labels
ggplot(mtcars, aes(x = wt, y = mpg, label = cyl)) +
geom_text()
# Define a hexadecimal color
my_color <- "#4ABEFF"

# Draw a scatter plot with color *aesthetic*


ggplot(mtcars, aes(x = wt, y = mpg, color = cyl)) +
geom_point()

# Same, but set color *attribute* in geom layer


ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point(color = my_color)

# Set the fill


aesthetic; color, size and shape attributes
ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(size = 10, shape = 23, color = my_color)

# Expand to draw points with alpha 0.5


ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(alpha = 0.5)

# Expand to draw points with shape 24 and color yellow


ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_point(shape = 24, color = 'yellow')

# Expand to draw text with label rownames(mtcars) and color red


ggplot(mtcars, aes(x = wt, y = mpg, fill = cyl)) +
geom_text(label = rownames(mtcars), color = 'red')
# Map mpg onto x, qsec onto y and factor(cyl) onto col (3
aesthetics):
ggplot(mtcars, aes(x = mpg, y = qsec, col = factor(cyl))) +
geom_point()

# Add mapping: factor(am) onto shape (now 4 aesthetics):


ggplot(mtcars, aes(x = mpg, y = qsec, col = factor(cyl), shape =
factor(am))) +
geom_point()

# Add mapping: (hp/wt) onto size (now 5 aesthetics):


ggplot(mtcars, aes(x = mpg, y = qsec, col = factor(cyl), shape =
factor(am), size = (hp/wt))) +
geom_point()
# The base layer, cyl.am, is available for you
# Add geom (position = "stack" by default)
cyl.am +
geom_bar()

# Fill - show proportion


cyl.am +
geom_bar(position = "fill")

# Dodging - principles of similarity and proximity


cyl.am +
geom_bar(position = "dodge")
# Clean up the axes with scale_ functions
val = c("#E41A1C", "#377EB8")
lab = c("Manual", "Automatic")
cyl.am +
geom_bar(position = "dodge") +
scale_x_discrete("Cylinders") +
scale_y_continuous("Number") +
scale_fill_manual("Transmission",
values = val,
labels = lab)

You might also like