Assignment No 808
Assignment No 808
Data Visualization
Practice Program:
1. Write an R program and an to draw an empty plot specifies the axis
limits of the graphic.
Solution:
# Set the limits for the x and y axes
xlim <- c(0, 10) # X-axis limits
ylim <- c(0, 100) # Y-axis limits
2. Using inbuilt air quality dataset make a scatter plot to compare Wind speed
and temperature
Solution:
# Load necessary library
library(ggplot2)
3. Using inbuilt iris dataset create Histogram for Petal. length values
Solution:
# Load necessary library
library(ggplot2)
4. Using iris dataset draw horizontal bar plate for Petal length for species. setosa
Solution: # Load necessary library
library(ggplot2)
SET A
1. Using inbuilt mtcars, dataset.
a. Create a bar plot for attribute mpg for all cars having 3 gears
b. Create histogram to show no of cars per carborattor type whose mpg
is greater than 20
Solution:
# Load necessary libraries
library(ggplot2)
# a) Create a bar plot for attribute mpg for all cars having 3 gears
cars_with_3_gears <- subset(mtcars, gear == 3)
# b) Create a histogram to show the number of cars per carburetor type whose mpg is
greater than 20
cars_high_mpg <- subset(mtcars, mpg > 20)
# Create a histogram
ggplot(cars_high_mpg, aes(x = as.factor(carb))) +
geom_bar(fill = "lightgreen") +
labs(title = "Number of Cars per Carburetor Type (MPG > 20)",
x = "Number of Carburetors",
y = "Count of Cars") +
theme_minimal()
# a) Scatter plot showing the relationship between Ozone and Wind values
ggplot(airquality, aes(x = Wind, y = Ozone, color = as.factor(Month))) +
geom_point(size = 3, alpha = 0.6) +
labs(title = "Scatter Plot of Ozone vs. Wind",
x = "Wind (mph)",
y = "Ozone (ppb)",
color = "Month") +
theme_minimal()
# b) Bar plot showing Ozone levels for all days with temperature > 70
high_temp_days <- airquality[airquality$Temp > 70, ]
SET B
1. Using inbuilt mtcars dataset.
a. Create a bar plot that shows the number of cars of each gear type.
b. Draw a scatter plot showing the relationship beth it & mpg for all the
cars having 4 gears
Solution:
# Load necessary libraries
library(ggplot2)
# b) Scatter plot showing the relationship between mpg and cyl for cars with 4 gears
# Filter the dataset for cars with 4 gears
four_gear_cars <- mtcars[mtcars$gear == 4, ]
SET C
1. Using inbuilt mtcars dataset show a stacker bar graph of the number of each gear type
& how They are further divided out by cyl
Solution:
# Load necessary library
library(ggplot2)