DATA VISUALIZATION

Data visualization is an essential part of data analysis and exploration in R. R offers a wide range of packages and tools for creating various types of data visualizations.

#One categorical and one continuous variable (Bar Chart)
# Sample data
data <- data.frame(Category = c("A", "B", "A", "C", "B"), Value = c(10, 15, 8, 20, 12))

# Create a bar chart
barplot(data$Value, names.arg = data$Category, col = "orange", main = "Categorical vs. Continuous", xlab = "Category", ylab = "Value")

#One continuous variable (Histogram)
# Sample data
data <- rnorm(100)

# Create a histogram
hist(data, col = "blue", main = "Histogram", xlab = "Value", ylab = "Frequency")

#Two continuous variables (Scatter Plot)
# Sample data
x <- rnorm(100)
y <- rnorm(100)

# Create a scatter plot
plot(x, y, col = "purple", main = "Scatter Plot", xlab = "X", ylab = "Y")

# Load the airquality dataset
data(airquality)

# Create a horizontal bar chart for the "Ozone" attribute
barplot(airquality$Ozone, names.arg = airquality$Month, col = "skyblue",
        main = "Ozone Distribution by Month", xlab = "Month", ylab = "Ozone")

A horizontal bar chart illustrates the distribution of ozone levels by month in the airquality dataset. It visually represents variations in ozone levels throughout the year, with higher concentrations in some months, mainly during month 8 and less during month 6.

# Create a histogram for the "Wind" attribute
hist(airquality$Wind, col = "orange", main = "Wind Speed Distribution", xlab = "Wind Speed", ylab = "Frequency")

The histogram provides a clear view of wind speed distribution in the airquality dataset. It shows that most wind speeds fall within around 10.

# Create a scatter plot for "Wind" vs. "Ozone"
plot(airquality$Wind, airquality$Ozone, col = "green", main = "Wind Speed vs. Ozone", xlab = "Wind Speed", ylab = "Ozone")

The scatter plot reveals the relationship between wind speed and ozone concentration. It suggests a lack of strong correlation between the two variables, as data points are scattered without a clear pattern, indicating that wind speed doesn’t significantly impact ozone levels.

basic graphical data analysis of mtcars dataset using basic plots:

bar chart shows the data in the form of bars.example:

# Bar chart showing the number of cars by the number of cylinders
barplot(table(mtcars$cyl), main = "Number of Cars by Cylinders", xlab = "Cylinders", ylab = "Count")

most of the cars contain 8 cylinders.

line plot connects different points with a line.example:

# Line plot showing the trend of car horsepower over time
plot(mtcars$hp, type = "l", main = "Horsepower Over Time", xlab = "Car Index", ylab = "Horsepower")

scatter plot scatters (x,y) points across the graph. example:

# Scatter plot showing the relationship between car weight and miles per gallon
plot(mtcars$wt, mtcars$mpg, main = "Car Weight vs. MPG", xlab = "Weight", ylab = "Miles per Gallon")

a pie chart plots the data as a share of a pie.example:

# Count the number of cars by the number of cylinders
cylinder_counts <- table(mtcars$cyl)

# Create a pie chart
pie(cylinder_counts, main = "Car Counts by Cylinders")

boxplot is used to find outliers in data and data is formed according to the percentiles.example:

# Box plot showing the distribution of car miles per gallon
boxplot(mtcars$mpg, main = "Miles per Gallon Distribution", ylab = "Miles per Gallon")

histogram is used to find frequency for a specific column in a dataset.example:

# Histogram showing the distribution of car horsepower
hist(mtcars$hp, main = "Horsepower Distribution", xlab = "Horsepower", ylab = "Frequency")