Program 07

Author

Manjunath B R

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.

library(ggplot2)

Step 2: Create data for the functions.

x <- seq(-2*pi, 2*pi, length.out = 500)
y1 <- sin(x)
y2 <- cos(x)

dt <- data.frame(
  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.

p <- ggplot(dt, aes(x = x, y = y, color = group, linetype = group))

Step 3.2: Add the line geometry

p <- p + geom_line(size = 1.2)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

Step 3.3: Add plot labels

p <- p + labs(
  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 <- p + theme_minimal()
p