Plots in R
Plots in R are visual representations of data that help analysts and data scientists explore, analyze, and communicate insights. R offers a rich ecosystem of plotting functions and libraries, each designed for specific data visualization needs.
###One categorical and one continuous variable (Bar Chart)
data <- data.frame(Category = c("A", "B", "A", "C", "B"), Value = c(10, 15, 8, 20, 12))
barplot(data$Value, names.arg = data$Category, col = "#FA9081", main = "Categorical vs. Continuous", xlab = "Category", ylab = "Value")
Histogram
data <- rnorm(100)
hist(data, col = "#49525E", main = "Histogram", xlab = "Value", ylab = "Frequency")
##Two continuous variables (Scatter Plot)
x <- rnorm(100)
y <- rnorm(100)
plot(x, y, col = "#00A396", main = "Scatter Plot", xlab = "X", ylab = "Y")
data(airquality)
# Create a horizontal bar chart for the "Ozone" attribute
barplot(airquality$Ozone, names.arg = airquality$Month, col = "#FFC2C5",
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 = "#D589B0", 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 = "#8B9FC7", 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 plots represent data using rectangular bars of varying lengths. They are ideal for visualizing categorical data and comparing values between different categories.
# 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",col="#3975B9")
Most of the cars contain 8 cylinders.
Line plots are used to visualize trends over time. They connect data points with lines, making them suitable for time series data or data with a natural ordering.
# Line plot showing the trend of car horsepower over time
plot(mtcars$hp, type = "l", main = "Horsepower Over Time", xlab = "Car Index", ylab = "Horsepower",col="#3F784D")
Scatter plots display individual data points as dots on a
two-dimensional graph. They are used to explore relationships between
two continuous variables, making it easy to identify patterns and
trends.
# 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")
Pie charts display parts of a whole by dividing a circle into segments.
While less commonly used for data visualization in R, they can represent
categorical data as percentages of the whole.
# 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")
Box plots display the distribution of data and its central tendency.
They show the median, quartiles, and potential outliers, making them
useful for comparing multiple groups or distributions.
# Box plot showing the distribution of car miles per gallon
boxplot(mtcars$mpg, main = "Miles per Gallon Distribution", ylab = "Miles per Gallon")
Pie charts display parts of a whole by dividing a circle into segments.
While less commonly used for data visualization in R, they can represent
categorical data as percentages of the whole.
# Histogram showing the distribution of car horsepower
hist(mtcars$hp, main = "Horsepower Distribution", xlab = "Horsepower", ylab = "Frequency",col="#9C93DB")