0% found this document useful (0 votes)
3 views8 pages

Assignment No 808

The document outlines a series of assignments focused on data visualization using R programming. It includes instructions and solutions for creating various plots such as empty plots, scatter plots, histograms, bar plots, and box plots using datasets like airquality and mtcars. Each section provides code snippets demonstrating how to visualize relationships between different variables and summarize data effectively.

Uploaded by

Sakshi Takwale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Assignment No 808

The document outlines a series of assignments focused on data visualization using R programming. It includes instructions and solutions for creating various plots such as empty plots, scatter plots, histograms, bar plots, and box plots using datasets like airquality and mtcars. Each section provides code snippets demonstrating how to visualize relationships between different variables and summarize data effectively.

Uploaded by

Sakshi Takwale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ASSIGNMENT NO 8

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

# Create an empty plot


plot(xlim, ylim, type = "n", xlab = "X Axis", ylab = "Y Axis",
main = "Empty Plot with Specified Axis Limits")

# Optionally, you can add grid lines for better visibility


grid()

2. Using inbuilt air quality dataset make a scatter plot to compare Wind speed
and temperature
Solution:
# Load necessary library
library(ggplot2)

# Load the airquality dataset


data("airquality")

# Create a scatter plot to compare Wind speed and Temperature


ggplot(airquality, aes(x = Wind, y = Temp)) +
geom_point(color = "blue", size = 2, alpha = 0.6) +
labs(title = "Scatter Plot of Wind Speed vs Temperature",
x = "Wind Speed (mph)",
y = "Temperature (°F)") +
theme_minimal()

3. Using inbuilt iris dataset create Histogram for Petal. length values
Solution:
# Load necessary library
library(ggplot2)

# Load the iris dataset


data("iris")

# Create a histogram for Petal Length


ggplot(iris, aes(x = Petal.Length)) +
geom_histogram(binwidth = 0.5, fill = "lightblue", color = "black") +
labs(title = "Histogram of Petal Length Values",
x = "Petal Length",
y = "Frequency") +
theme_minimal()

4. Using iris dataset draw horizontal bar plate for Petal length for species. setosa
Solution: # Load necessary library
library(ggplot2)

# Load the iris dataset


data("iris")

# Filter the dataset for species setosa


setosa_data <- subset(iris, Species == "setosa")
# Create a horizontal bar plot for Petal Length
ggplot(setosa_data, aes(x = Petal.Length)) +
geom_bar(stat = "count", fill = "lightblue") +
labs(title = "Petal Length for Species Setosa",
x = "Petal Length",
y = "Count") +
theme_minimal() +
coord_flip() # Flip the coordinates to make it horizontal

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)

# Load the mtcars dataset


data("mtcars")

# a) Create a bar plot for attribute mpg for all cars having 3 gears
cars_with_3_gears <- subset(mtcars, gear == 3)

# Create a bar plot for mpg of cars with 3 gears


ggplot(cars_with_3_gears, aes(x = as.factor(row.names(cars_with_3_gears)), y = mpg)) +
geom_bar(stat = "identity", fill = "steelblue") +
labs(title = "MPG of Cars with 3 Gears",
x = "Car Models",
y = "Miles Per Gallon (MPG)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + # Rotate x-axis labels for
readability
theme_minimal()

# 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()

2. Using air quality dataset.


a. Create a scatter plot to show the relation -ship bet ozone & Wind values by giving
appropriate value to colour arguments
b. Create a bar plot to show the ozone level for all days having temp. > 70
Solution:
# Load necessary libraries
library(ggplot2)

# Load the airquality dataset


data("airquality")

# 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, ]

ggplot(high_temp_days, aes(x = as.factor(Day), y = Ozone)) +


geom_bar(stat = "identity", fill = "lightblue") +
labs(title = "Ozone Levels for Days with Temperature > 70",
x = "Day of the Month",
y = "Ozone (ppb)") +
theme_minimal()

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)

# Load the mtcars dataset


data("mtcars")
# a) Bar plot showing the number of cars of each gear type
ggplot(mtcars, aes(x = as.factor(gear))) +
geom_bar(fill = "lightblue") +
labs(title = "Number of Cars by Gear Type",
x = "Number of Gears",
y = "Number of Cars") +
theme_minimal()

# 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, ]

ggplot(four_gear_cars, aes(x = cyl, y = mpg)) +


geom_point(color = "darkorange", size = 3) +
labs(title = "Scatter Plot of MPG vs. Cylinders for Cars with 4 Gears",
x = "Number of Cylinders",
y = "Miles Per Gallon (MPG)") +
theme_minimal() +
geom_smooth(method = "lm", se = FALSE, color = "blue") # Add a regression line

2. Using Air quality dataset


a. shows the statistical summary using box plot for Temperature value of month
June
b. Using histogram show the Frequency of no. of days for Temp.
values of month August
Solution:
# Load necessary libraries
library(ggplot2)

# Load the airquality dataset


data("airquality")

# a) Box plot for temperature values of June


# Filter the dataset for June (Month == 6)
june_data <- airquality[airquality$Month == 6, ]

# Create a box plot for Temperature in June


ggplot(june_data, aes(y = Temp)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Box Plot of Temperature in June",
y = "Temperature (°F)") +
theme_minimal()

# b) Histogram for the frequency of temperature values for August


# Filter the dataset for August (Month == 8)
august_data <- airquality[airquality$Month == 8, ]

# Create a histogram for Temperature in August


ggplot(august_data, aes(x = Temp)) +
geom_histogram(binwidth = 2, fill = "lightgreen", color = "black") +
labs(title = "Histogram of Temperature in August",
x = "Temperature (°F)",
y = "Frequency") +
theme_minimal()

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)

# Load the mtcars dataset


data("mtcars")

# Create a stacked bar graph


ggplot(mtcars, aes(x = factor(gear), fill = factor(cyl))) +
geom_bar() +
labs(title = "Stacked Bar Graph of Gear Types and Cylinders",
x = "Number of Gears",
y = "Count of Cars",
fill = "Number of Cylinders") +
theme_minimal()

2. Using boxplot to show the distribution of mpg values per no of gears.


Solution:
# Load the mtcars dataset
data("mtcars")

# Create a boxplot of mpg values grouped by the number of gears


boxplot(mpg ~ gear, data = mtcars,
main = "Distribution of MPG Values by Number of Gears",
xlab = "Number of Gears",
ylab = "Miles Per Gallon (MPG)",
col = "lightblue",
border = "darkblue")

You might also like