library(ggplot2)
Program 07
Quarto
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
Step 1: Load the Libraries.
Step 2: Create data for the functions.
<- seq(-2*pi, 2*pi, length.out = 500)
x <- sin(x)
y1 <- cos(x)
y2
<- data.frame(
dt x = rep(x, 2),
y = c(y1, y2),
group = rep(c("sin(x)", "cos(x)"), each = length(x))
)
library(ggplot2)
ggplot(dt, aes(x = x, y = y, color = group)) +
geom_line() +
theme_minimal() +
labs(title = "sin(x) and cos(x)", x = "x", y = "y")
Step 3: Create function to create a Graph.
Step 3.1: Initialize the ggplot object.
<- ggplot(dt, aes(x = x, y = y, color = group, linetype = group)) p
Step 3.2: Add the line geometry
<- p + geom_line(size = 1.2) p
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Step 3.3: Add plot labels
<- p + labs(
p title = "Function curves: sin(x) & cos(x)",
x = "x",
y = "y = f(x)",
color = "Function",
linetype = "Function"
) p
Step 3.4: Apply a neat theme to display graph
<- p + theme_minimal()
p p