plots in r

R language is mostly used for statistics and data analytics purposes to represent the data graphically in the software. To represent those data graphically, plots, charts and graphs are used 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", "A", "B", "C", "B"), Value = c(11, 17, 21, 23, 19))

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

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

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

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

# Create a scatter plot
plot(x, y, col = "red", main = "Scatter Plot", xlab = "value", ylab = "Distribution")

# Load the iris dataset
data("iris")

# Create a horizontal bar chart for the "sepal.length vs sepal.width" attribute
barplot(iris$Sepal.Width, names.arg = airquality$Sepal.Length, col = "yellow",
        main = "Sepal.Width by Sepal length", xlab = "sepal width", ylab = "sepal length")

# Create a histogram for the "Sepal length" attribute
hist(iris$Petal.Length, col = "green", main = "Sepal length Distribution", xlab = "Sepal length", ylab = "Frequency")

The histogram provides a clear view of Sepal length distribution in the iris data set. It shows that same sepal length fall is in range 4 to 5.

# Create a scatter plot for "Sepal length" vs. "petal length"
plot(iris$Petal.Length, iris$Sepal.Length, col = "red", main = "Petal length  vs. sepal length", xlab = "Petal Length", ylab = "sepal length")

The scatter plot reveals the relationship between sepal length and petal length. It suggests a lack of strong correlation between the two variables, as data points are scattered without a clear pattern, indicating that sepal length and petal lengths vary large for same and different species.

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

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

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$hp, main = "Car Weight vs. hp", xlab = "Weight", ylab = "Horse Power")

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$hp)

# 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$cyl, main = "cars by cylinder", ylab = "count of cars")

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

The above are some of the plot used to visually represent the data,These plots can be used to analyse the data pictographically.