# Clear environment, plot pane, and console
rm(list = ls())
graphics.off()
cat("\014")
# If pacman is not already installed, then install it
if (!require(pacman)) install.packages("pacman")
# Load packages
pacman::p_load(ggplot2, data.table)
# Colors used for plot (https://www.rapidtables.com/web/color/RGB_Color.html)
color_1 <- "#00FF00"
color_2 <- "#ADD8E6"
# Define beta_0, beta_1, x, y
beta_0 <- 5
beta_1 <- 2
x <- c(0:10)
y <- beta_0 + (beta_1 * x)
# Put x and y into a data table
dt <- data.table(x, y)
# Create a line plot
ggplot(dt, aes(x, y)) +
geom_line(linewidth = 1, color = color_1) +
labs(
title = "Linear Function",
subtitle = expression(y == beta[0] + beta[1] * x)
) +
theme(
panel.background = element_rect(fill = "black", color = "black"),
plot.background = element_rect(fill = "black", color = "black"),
panel.grid.major = element_line(color = "gray"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = color_1, size = 15),
axis.title = element_text(color = color_2, size = 25),
plot.title = element_text(hjust = 0.5, color = color_2, size = 30, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, color = color_1, size = 25)
) +
expand_limits(y = 0) +
xlab("x") +
ylab("y")
It’s likely you looked at the above code and had no idea what is
going on. That’s totally fine! As I said in the R tutorial document
which is located on D2L
under
Content
>R and RStudio
>ECON_418-518_R_Tutorial
,
when you read some code you can’t understand or need to figure out how
to write some code, use ChatGPT
! I always have
ChatGPT
up when I code and it has increased my coding
efficiency ten fold. In fact, I used ChatGPT
to create that
plot!
Please do the following:
ChatGPT 3.5
is currently free). Otherwise, log into your
account.Message ChatGPT
. In that box, type
What is ggplot2? Also, give me an example of how to use it.
Then, press Enter
.You should get a fairly detailed definition of ggplot2
package as well as an example of how to use it. You can do this for
essentially any coding question you may have. Now,
Message ChatGPT
box. Press the space bar and type
Explain this R code
and press Enter
.Again, you should get a fairly detailed description of the code and
what each aspect of it does. Click here to see my conversation with
ChatGPT
about ggplot2
and what the above code
does to see for yourself how powerful it is.
I highly encourage you to take advantage of ChatGPT
throughout this course as well as when you get into the professional
world!
# Define beta_0, beta_1, beta_2, age, wage
beta_0 <- 100
beta_1 <- 20
beta_2 <- 0.4
age <- c(30:70)
wage <- beta_0 - (beta_1 - beta_2 * age)^2
# Put age and wage into a data table
dt <- data.table(age, wage)
# Create a line plot
ggplot(dt, aes(age, wage)) +
geom_line(linewidth = 1, color = color_1) +
labs(
title = "Non-Linear Function",
subtitle = expression(Wage == beta[0] - (beta[1] + beta[2] * Age)^2)
) +
theme(
panel.background = element_rect(fill = "black", color = "black"),
plot.background = element_rect(fill = "black", color = "black"),
panel.grid.major = element_line(color = "gray"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = color_1, size = 15),
axis.title = element_text(color = color_2, size = 25),
plot.title = element_text(hjust = 0.5, color = color_2, size = 30, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, color = color_1, size = 25)
) +
expand_limits(y = 0) +
xlab("Age") +
ylab("Wage (In Thousands)")
# Define beta_0, beta_1, age, wage
beta_0 <- 0.5
beta_1 <- 0.5
hours_studied <- c(0.5:20)
gpa <- beta_0 + beta_1 + log(hours_studied)
# Put age and wage into a data table
dt <- data.table(hours_studied, gpa)
# Create a line plot
ggplot(dt, aes(hours_studied, gpa)) +
geom_line(linewidth = 1, color = color_1) +
labs(
title = "Natural Logarithm Function",
subtitle = expression(GPA == beta[0] + beta[1] * log(Hours~Studied))
) +
theme(
panel.background = element_rect(fill = "black", color = "black"),
plot.background = element_rect(fill = "black", color = "black"),
panel.grid.major = element_line(color = "gray"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = color_1, size = 15),
axis.title = element_text(color = color_2, size = 25),
plot.title = element_text(hjust = 0.5, color = color_2, size = 30, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, color = color_1, size = 25)
) +
expand_limits(y = 0) +
xlab("Hours Studied per Week") +
ylab("GPA")
# Create a data table with the normal distributions
means <- c(0, 1, 2, 3)
sds <- c(1, 0.5, 1.5, 2)
x <- seq(-10, 10, length.out = 100)
dt <- data.table(
x = rep(x, times = 4),
y = c(dnorm(x, mean = means[1], sd = sds[1]),
dnorm(x, mean = means[2], sd = sds[2]),
dnorm(x, mean = means[3], sd = sds[3]),
dnorm(x, mean = means[4], sd = sds[4])),
group = factor(rep(1:4, each = 100))
)
# Create the line plot
ggplot(dt, aes(x, y, color = group)) +
geom_line(linewidth = 1) +
labs(
title = "Normal Distributions",
color = "Distribution" # Legend title
) +
scale_color_manual(values = c("#FFA07A", "#20B2AA", "#778899", "#9370DB"),
labels = c("Mean=0, SD=1", "Mean=1, SD=0.5", "Mean=2, SD=1.5", "Mean=3, SD=2")) +
theme(
panel.background = element_rect(fill = "black", color = "black"),
plot.background = element_rect(fill = "black", color = "black"),
panel.grid.major = element_line(color = "gray"),
panel.grid.minor = element_line(color = "gray"),
axis.text = element_text(color = color_1, size = 15),
axis.title = element_text(color = color_2, size = 25),
plot.title = element_text(hjust = 0.5, color = color_2, size = 30, face = "bold"),
plot.subtitle = element_text(hjust = 0.5, color = color_1, size = 25),
legend.background = element_rect(fill = "black"),
legend.text = element_text(color = color_1, size = 15),
legend.title = element_text(color = color_2, size = 20)
) +
expand_limits(y = 0) +
xlab("x") +
ylab("Density")