Program 08

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:

1 + 1
[1] 2

You can add options to executable code like this

# Load required libraries
library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
# ------------------------------
# Program 1: Quick Dataset Exploration with Categorical Analysis
# ------------------------------
# Using 'mtcars' dataset
df1 <- mtcars
df1$cyl <- as.factor(df1$cyl)

# Group by 'cyl' and summarize
summary_stats <- df1 %>%
  group_by(cyl) %>%
  summarise(
    count = n(),
    avg_mpg = mean(mpg),
    avg_hp = mean(hp)
  )
print(summary_stats)
# A tibble: 3 × 4
  cyl   count avg_mpg avg_hp
  <fct> <int>   <dbl>  <dbl>
1 4        11    26.7   82.6
2 6         7    19.7  122. 
3 8        14    15.1  209. 
# Visualization
ggplot(df1, aes(x = cyl, fill = cyl)) +
  geom_bar() +
  labs(title = "Count of Cars by Cylinder", x = "Cylinders", y = "Count") +
  theme_minimal()

# ------------------------------
# Program 2: Scatter Plot with Categorical Coloring
# ------------------------------
ggplot(df1, aes(x = hp, y = mpg, color = cyl)) +
  geom_point(size = 3) +
  labs(title = "MPG vs HP colored by Cylinders", x = "Horsepower", y = "Miles per Gallon") +
  theme_minimal()

# ------------------------------
# Program 3: Line Graph for Time-Series by Group
# ------------------------------
# Simulate time-series data
set.seed(123)
time <- rep(1:10, 3)
group <- rep(c("A", "B", "C"), each = 10)
value <- cumsum(rnorm(30))
ts_data <- data.frame(time, group, value)

ggplot(ts_data, aes(x = time, y = value, color = group, group = group)) +
  geom_line(size = 1.2) +
  labs(title = "Simulated Time-Series Trends", x = "Time", y = "Value") +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

# ------------------------------
# Program 4: Bar Graph of Frequency Distribution
# ------------------------------
# Use 'mtcars' dataset again
ggplot(df1, aes(x = gear, fill = cyl)) +
  geom_bar(position = "dodge") +
  labs(title = "Gear Frequency by Cylinder", x = "Gears", y = "Count") +
  theme_minimal()

# ------------------------------
# Program 5: Box Plot with Fill for Categorical Groups
# ------------------------------
ggplot(df1, aes(x = cyl, y = mpg, fill = cyl)) +
  geom_boxplot() +
  labs(title = "MPG Distribution by Cylinders", x = "Cylinders", y = "MPG") +
  theme_minimal()

ndsj

# ------------------------------
# Program 6: Sine and Cosine Line Graph
# ------------------------------
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))
)

ggplot(dt, aes(x = x, y = y, color = group)) +
  geom_line(size = 1.2) +
  labs(title = "Function Curves: sin(x) and cos(x)", x = "x", y = "f(x)") +
  theme_minimal()

The echo: false option disables the printing of code (only output is displayed).