0% found this document useful (0 votes)
18 views6 pages

R Programming: Pie & Bar Chart Examples

r program
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)
18 views6 pages

R Programming: Pie & Bar Chart Examples

r program
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

R Programming 5th Experiment

. Write an R program to draw i) Pie chart ii) 3D Pie Chart, iii) Bar
Chart along with chart legend by considering suitable CSV file

Procedure:
1. pie chart:
i. Create the Data:
• We first define two vectors: Category and Value.
• Category contains names of different fruits.
• Value represents some numeric value associated with each fruit.
• Create a Data Frame:
We then combine the Category and Value vectors into a data frame called data.

Program:

# Load necessary library


library(ggplot2)

# Create data
Category <- c("Apples", "Bananas", "Cherries", "Dates", "Elderberries",
"Figs", "Grapes", "Honeydew", "Indian Fig", "Jackfruit")
Value <- c(15, 25, 10, 30, 5, 20, 35, 10, 5, 45)

# Create a data frame


data <- [Link](Category, Value)

# Draw the pie chart


ggplot(data, aes(x = "", y = Value, fill = Category)) +
geom_bar(stat = "identity", width = 1) +
coord_polar("y", start = 0) +
theme_void() +
labs(title = "Pie Chart with 10 Records") +
theme([Link] = "right")
Output:
ii) 3D Pie Chart

Program:

# Load the required package


library(plotrix)

# Sample data for the 3D pie chart (10 records)


data <- [Link](
Category = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
Value = c(10, 15, 20, 25, 5, 12, 18, 30, 8, 7)
)

# Create the 3D pie chart


pie3D(data$Value,
labels = data$Category,
main = "3D Pie Chart of Categories",
explode = 0.1, # Slightly separate the slices
col = rainbow(length(data$Value))) # Color palette for slices

# Add a legend to the chart


legend("topright",
legend = data$Category,
fill = rainbow(length(data$Value)),
cex = 0.8) # Adjust text size in the legend
Output:
[Link] chart

# Load the required library


library(ggplot2)

# Sample data for the bar chart (10 records)


data <- [Link](
Category = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
Value = c(10, 15, 20, 25, 5, 12, 18, 30, 8, 7)
)

# Create the bar chart


bar_plot <- ggplot(data, aes(x = Category, y = Value, fill = Category)) +
geom_bar(stat = "identity") + # Use the values for the height of the bars
labs(title = "Bar Chart of Categories", x = "Category", y = "Value") + # Add titles
and labels
theme_minimal() + # Use a minimal theme for cleaner appearance
theme([Link] = "none") # Remove the legend

# Display the bar chart


print(bar_plot)
Output:

You might also like