0% found this document useful (0 votes)
7 views14 pages

Pie Chart and Bar Chart

The document provides various methods for creating different types of charts in R, including pie charts, bar charts, scatter plots, histograms, line graphs, and multiple-line graphs using both Base R and ggplot2. Each section includes example code, explanations of parameters, and notes on customization options. It emphasizes the use of ggplot2 for enhanced visual customization compared to Base R functions.

Uploaded by

akashkr0802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Pie Chart and Bar Chart

The document provides various methods for creating different types of charts in R, including pie charts, bar charts, scatter plots, histograms, line graphs, and multiple-line graphs using both Base R and ggplot2. Each section includes example code, explanations of parameters, and notes on customization options. It emphasizes the use of ggplot2 for enhanced visual customization compared to Base R functions.

Uploaded by

akashkr0802
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Pie chart

In R, you can create a pie chart using the pie() function or ggplot2 for more
customization. Here are two common methods:

EXAMPLE 1: USING PIE()


# Data
values <- c(30, 20, 50)
labels <- c("Apples", "Bananas", "Cherries")
# Create pie chart
pie(values, labels = labels, col = rainbow(length(values)), main = "Fruit Distribution")

output

EXAMPLE 2: USING GGPLOT()


# Load library
library(ggplot2)
# Data
data <- data.frame(
category = c("Apples", "Bananas", "Cherries"),
count = c(30, 20, 50)
)
# Create pie chart
ggplot(data, aes(x = "", y = count, fill = category)) +
geom_bar(stat = "identity", width = 1) +
coord_polar(theta = "y") +
theme_void() +
ggtitle("Fruit Distribution")

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

# Create 3D pie chart


pie3D(values, labels = labels, explode = 0.1, col = colors, main = "3D Pie Chart")

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

GROUP BAR PLOT


To create a grouped bar chart in R, you can use either Base R (barplot()) or
ggplot2 for more advanced customization.

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:

 matrix(c(...), nrow = 2, byrow = TRUE) creates a matrix of


values.
 beside = TRUE ensures bars are grouped, not stacked.
 col = c("blue", "red") sets colors for each group.
 legend = rownames(values) adds a legend.

OUTPUT

ggplot2: More Customizable Grouped Bar Chart

# 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

To plot data points in different color


# 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, color = as.factor(x))) + 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

ggplot2: More Customizable Histogram


# Load library
library(ggplot2)

df <- data.frame(values = rnorm(1000, mean = 50, sd = 10))


# Create histogram
ggplot(df, aes(x = values))
+ geom_histogram(binwidth = 5, fill = "blue", color = "black", alpha = 0.7)
+ ggtitle("Histogram of Data")
+ xlab("Values")
+ ylab("Frequency")
+ theme_minimal()
OUTPUT

LINE GRAPH

To create a line graph in R, you can use either Base R (plot()) or ggplot2 for better
customization.

1. Base R: Using plot()

# Sample data
x <- 1:10
y <- c(2, 5, 7, 8, 12, 15, 20, 22, 24, 30)

# Create line graph


plot(x, y, type = "o", col = "blue", lwd = 2, pch = 16,
main = "Line Graph in Base R", xlab = "X Axis", ylab = "Y Axis")

# Add grid
grid()

NOTE

 type = "o": Both points and lines are plotted.


 lwd = 2: Increases line width.
 pch = 16: Changes point shape.
 grid(): Adds background grid.
OUTPUT

2. ggplot2: More Customizable Line Graph

# Load library
library(ggplot2)

# Create data
df <- data.frame(
x = 1:10,
y = c(2, 5, 7, 8, 12, 15, 20, 22, 24, 30)
)

# Create line graph


ggplot(df, aes(x = x, y = y)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
ggtitle("Line Graph with ggplot2") +
xlab("X Axis") + ylab("Y Axis") +
theme_minimal()

NOTE

 geom_line() draws the line.


 geom_point() adds points.
 Custom styling options (colors, themes, labels).
OUTPUT

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

y1 <- c(2, 5, 7, 8, 12, 15, 20, 22, 24, 30)

y2 <- c(3, 6, 8, 10, 13, 17, 19, 23, 27, 32)

# Create an empty plot

plot(x, y1, type = "o", col = "blue", lwd = 2, pch = 16,

ylim = c(0, 35), xlab = "X Axis", ylab = "Y Axis",

main = "Multiple Line Graph in Base R")

# Add second line

lines(x, y2, type = "o", col = "red", lwd = 2, pch = 17)


# Add legend

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.

ggplot2: More Customizable Multiple Line Graph


# Load library
library(ggplot2)
# Create data
df <- data.frame(
x = rep(1:10, 2),
y = c(2, 5, 7, 8, 12, 15, 20, 22, 24, 30,
3, 6, 8, 10, 13, 17, 19, 23, 27, 32),
group = rep(c("Line 1", "Line 2"), each = 10) # Group identifier
)
# Create multiple line graph
ggplot(df, aes(x = x, y = y, color = group, group = group)) +
geom_line(size = 1) +
geom_point(size = 3) +
ggtitle("Multiple Line Graph with ggplot2") +
xlab("X Axis") + ylab("Y Axis") +
theme_minimal()
NOTE
 Automatically handles multiple lines using group.
 Color-coded lines for better visualization.
 Easy to customize themes, legends, and labels.

OUTPUT

You might also like