0% found this document useful (0 votes)
7 views3 pages

Shahadat

R code

Uploaded by

Shahadat Molla
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)
7 views3 pages

Shahadat

R code

Uploaded by

Shahadat Molla
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/ 3

# Answer 1; Generate a line plot without confidence intervals:

library(ggplot2)
# Line plot without confidence intervals
ggplot(mpg, aes(x=displ, y=hwy, color=drv)) +
geom_smooth(method="lm", se=FALSE) +
labs(title="Engine Displacement vs Highway Mileage",
x="Engine Displacement (L)", y="Highway Mileage (mpg)")

# Answer 2: Scatter plot with blue points:

# Scatter plot with blue points


ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point(color="blue") +
labs(title="Engine Displacement vs Highway Mileage",
x="Engine Displacement (L)", y="Highway Mileage (mpg)")

# Answer 3: Scatter plot with a trend line:

# Scatter plot with trend line


ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point() +
geom_smooth(method="lm") +
labs(title="Engine Displacement vs Highway Mileage",
x="Engine Displacement (L)", y="Highway Mileage (mpg)")

# Answer 4: Facet wrap by class:

# Facet wrap by vehicle class


ggplot(mpg, aes(x=displ, y=hwy)) +
geom_point() +
facet_wrap(~ class) +
labs(title="Engine Displacement vs Highway Mileage by Class",
x="Engine Displacement (L)", y="Highway Mileage (mpg)")

#Answer 5: Use drv (f, r, 4-wheel drive):

# Color by drive type (drv) with points


ggplot(mpg, aes(x=displ, y=hwy, color=drv)) +
geom_point() +
labs(title="Engine Displacement vs Highway Mileage by Drive Type",
x="Engine Displacement (L)", y="Highway Mileage (mpg)")

# Answer 6:
ggplot(mpg, aes(x = class, y = hwy)) +
geom_boxplot() +
labs(title = "Highway Mileage by Vehicle Class",
x = "Vehicle Class",
y = "Highway Mileage (mpg)") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Answer 7:
ggplot(mpg, aes(x = class)) +
geom_bar() +
labs(title = "Distribution of Vehicle Classes",
x = "Vehicle Class",
y = "Count")

# Answer 8:

ggplot(mpg, aes(x = displ, y = hwy)) +


geom_point() +
facet_wrap(~ class) +
labs(title = "Engine Displacement vs Highway Mileage by Class",
x = "Engine Displacement (L)",
y = "Highway Mileage (mpg)")

# Answer 9:

# 9.1 Structure of the dataset


str(mpg)

# 9.2 Missing values count


sapply(mpg, function(x) sum(is.na(x)))

# 9.3 Removing rows with missing values


mpg_clean <- na.omit(mpg)

# Answer:10
library(dplyr)
# 10.1 Create a new column for total fuel consumption
mpg_clean <- mpg_clean %>%
mutate(total_fuel_consumption = cty + hwy)

# 10.2 Group by vehicle class and calculate averages


avg_data <- mpg_clean %>%
group_by(class) %>%
summarise(avg_engine_size = mean(displ),
avg_fuel_consumption = mean(total_fuel_consumption))

# 10.3 Filter for vehicle classes with avg engine size > 3.0L
large_engine_classes <- avg_data %>%
filter(avg_engine_size > 3.0)

You might also like