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

Data Visulization

The document outlines a data analysis process using the 'mtcars' dataset in R, including importing the dataset, checking its structure, and generating summary statistics. It creates various visualizations such as a scatter plot for horsepower vs. MPG, a histogram for MPG distribution, a bar chart for cylinder counts, and a box plot comparing MPG across different cylinder types. The visualizations utilize the ggplot2 library to enhance data interpretation.

Uploaded by

Mike Tresford
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)
18 views3 pages

Data Visulization

The document outlines a data analysis process using the 'mtcars' dataset in R, including importing the dataset, checking its structure, and generating summary statistics. It creates various visualizations such as a scatter plot for horsepower vs. MPG, a histogram for MPG distribution, a bar chart for cylinder counts, and a box plot comparing MPG across different cylinder types. The visualizations utilize the ggplot2 library to enhance data interpretation.

Uploaded by

Mike Tresford
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

# Load required library

library(ggplot2)

# -----------------------------

# 1. Import Dataset

# -----------------------------

# The built-in 'mtcars' dataset is used (no need to import externally)


data <- mtcars

# View first few rows

head(data)

# Check structure of the dataset

str(data)

# Summary statistics

summary(data)

# Convert 'cyl' (cylinders) into a categorical variable

data$cyl <- as.factor(data$cyl)

# -----------------------------
# 2. Scatter Plot: Horsepower vs. MPG

# -----------------------------
# This scatter plot shows the relationship between Horsepower (hp) and Miles Per
Gallon (mpg)

ggplot(data, aes(x = hp, y = mpg)) +


geom_point(color = "blue") +

ggtitle("Scatter Plot: Horsepower vs. MPG") +

xlab("Horsepower (hp)") +

ylab("Miles Per Gallon (mpg)") +

theme_minimal()

# -----------------------------

# 3. Histogram: Distribution of MPG

# -----------------------------

# A histogram helps to see how MPG values are distributed in the dataset

ggplot(data, aes(x = mpg)) +

geom_histogram(binwidth = 2, fill = "green", color = "black") +

ggtitle("Histogram: Distribution of MPG") +

xlab("Miles Per Gallon (MPG)") +


ylab("Frequency") +
theme_minimal()

# -----------------------------

# 4. Bar Chart: Count of Cylinders

# -----------------------------

# A bar chart to visualize the number of cars for each cylinder type

ggplot(data, aes(x = cyl, fill = cyl)) +


geom_bar() +

ggtitle("Bar Chart: Count of Cars by Cylinder Type") +

xlab("Number of Cylinders") +

ylab("Count of Cars") +

theme_minimal()

# -----------------------------

# 5. Box Plot: MPG for Different Cylinders


# -----------------------------

# A box plot to compare MPG across different cylinder types

ggplot(data, aes(x = cyl, y = mpg, fill = cyl)) +

geom_boxplot() +

ggtitle("Box Plot: MPG by Cylinder Type") +

xlab("Number of Cylinders") +

ylab("Miles Per Gallon (MPG)") +

theme_minimal()

You might also like