Pie Chart and Bar Chart
Pie Chart and Bar Chart
In R, you can create a pie chart using the pie() function or ggplot2 for more
customization. Here are two common methods:
output
OUTPUT
Note:
In R, aes() (aesthetic mapping) is a function from the ggplot2 package used to
define how variables in your dataset are mapped to visual properties in a plot. This
aesthetics can include x and y axes, color, size, shape, fill, and more.
3D pie chart
install.packages("plotrix")
library(plotrix)
# Data
values <- c(30, 20, 50)
labels <- c("Apples", "Bananas", "Cherries")
colors <- c("red", "yellow", "pink")
OUTPUT
NOTE:
explode = 0.1 adds a slight separation between slices.
col = colors sets custom colors.
labels = labels adds text labels.
main = "3D Pie Chart" sets the title.
Bar chart
# Load library
library(ggplot2)
df <- data.frame( category = c("A", "B", "C"), value = c(10, 20, 15) )
ggplot(df, aes(x = category, y = value, fill = category)) + geom_bar(stat = "identity")
OUTPUT
USING BARPLOT()
values <- matrix(c(10, 15, 20, 25, 18, 22), nrow = 2, byrow = TRUE)
colnames(values) <- c("A", "B", "C")
rownames(values) <- c("Group 1", "Group 2")
# Create grouped bar plot
barplot(values, beside = TRUE, col = c("blue", "red"), legend = rownames(values),
main = "Grouped Bar Chart", xlab = "Categories", ylab = "Values")
NOTE:
OUTPUT
# Load library
library(ggplot2)
#DATA
df <- data.frame( Category = rep(c("A", "B", "C"), each = 2),
Group = rep(c("Group 1", "Group 2"), times = 3),
Value = c(10, 15, 20, 25, 18, 22) )
# Create grouped bar chart
ggplot(df, aes(x = Category, y = Value, fill = Group))
+ geom_bar(stat = "identity", position = "dodge")
+ ggtitle("Grouped Bar Chart")
+ xlab("Categories")
+ ylab("Values")
+ theme_minimal()
NOTE:
geom_bar(stat = "identity") plots actual values.
position = "dodge" makes bars grouped (not stacked).
More customization options for themes, colors, and labels.
OUTPUT
Scatter plot
# Load library
library(ggplot2)
# Data
data <- data.frame( x = c(1, 2, 3, 4, 5), y = c(3, 7, 2, 9, 4) )
# Scatter plot
ggplot(data, aes(x = x, y = y)) + geom_point()
output
HISTOGRAM
In R, you can create a histogram using either Base R (hist()) or ggplot2 for
more customization.
# Data
data <- rnorm(1000, mean = 50, sd = 10) # 1000 values from normal distribution
# Create histogram
hist(data, main = "Histogram of Data", xlab = "Values", ylab = "Frequency", col =
"skyblue", border = "black", breaks = 20) # Adjust the number of bins
NOTE:
rnorm(1000, mean = 50, sd = 10) generates 1000 random numbers.
breaks = 20 controls the number of bins (bars).
col = "skyblue" sets the bar color.
border = "black" sets the bar outline color.
OUTPUT
LINE GRAPH
To create a line graph in R, you can use either Base R (plot()) or ggplot2 for better
customization.
# Sample data
x <- 1:10
y <- c(2, 5, 7, 8, 12, 15, 20, 22, 24, 30)
# Add grid
grid()
NOTE
# Load library
library(ggplot2)
# Create data
df <- data.frame(
x = 1:10,
y = c(2, 5, 7, 8, 12, 15, 20, 22, 24, 30)
)
NOTE
MULTIPLE-LINE GRAPH
To create a multiple-line graph in R, you can use either Base R (plot() + lines()) or
ggplot2 for better customization.
# Sample data
x <- 1:10
legend("topleft", legend = c("Line 1", "Line 2"), col = c("blue", "red"), lty = 1, pch =
c(16, 17))
NOTES:
plot() creates the first line.
lines() adds another line to the same plot.
legend() adds a legend.
OUTPUT