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)
# Sample data
data <- data.frame(Category = c("A", "C", "A", "B", "C"), Value = c(28, 11, 22, 9, 14))

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

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

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

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

# 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 = "lightpink",
        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 = "purple", 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 = "red", 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:

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.

# Histogram of Miles Per Gallon (mpg)
hist(mtcars$mpg, main = "Histogram of MPG", xlab = "MPG")

# Boxplot of MPG by Number of Cylinders
boxplot(mpg ~ cyl, data = mtcars, main = "MPG by Number of Cylinders", xlab = "Cylinders", ylab = "MPG")

#Conclusion:

Cars with fewer cylinders tend to have higher mileage.

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")

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.

# Scatterplot of MPG against Weight (wt)
plot(mtcars$wt, mtcars$mpg, main = "Scatterplot: MPG vs Weight", xlab = "Weight", ylab = "MPG")

#Conclusion:

There seems to be a negative relationship between weight and fuel efficiency. Heavier cars tend to have lower mpg.

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")