Example-1:- Standard/ Basic Bar Chart
You have the final grades (out of 100) for five students in a class. Create a basic bar plot to visualize these scores
Data:- grades <- c(85, 92, 78, 95, 88)
Student Names:- students <- c("Alice", "Bob", "Charlie", "David", "Eve")
Label the x-axis "Student Name"
Label the y-axis "Score (out of 100)"
Set the main title to "Final Exam Results"
Use a colour of your choice
Code:-
# Data: Monthly sales figures
grades <- c(85, 92, 78, 95, 88)
# Labels for the x-axis
students <- c("Alice", "Bob", "Charlie", "David", "Eve")
# Create the bar plot
barplot(grades,
names.arg = students, # Add labels to the x-axis
main = "Final Exam Results", # Add a title
xlab = "Student Name", # Label the x-axis
ylab = "Score (out of 100)", # Label the y-axis
col = rainbow(length(students))) # Set color
Solution:-
Explanation:-
- Here you see in 2nd line and 5th line after grades & students you see <- c here c is compulsory to use to compile the data
- In 8th line we use all the parameter of barplot, names.arg= use to label on x-axis with student name as you see in above solution image
- main= use for give the title name (Final Exam Results) , xlab= use to label x-axis (Student Name)
- col use for colour here rainbow(length(students) means students length is 5. So, 5 different colour use here
- Also, in result image you can't see all 5 names (Alice, bob, charlie, david, eve)-- you can see david name missing in result, you simply click on zoom option then you can see all names, it basically due to less space.
--> All parameter of barplot()
Essential Data & Layout:-
Styling & Appearance:-
Axes & Scaling:-
Advanced Customization for Legend Box (explain later):-
Example-2:- How to use barplot parameter
Here I use some parameter like density, space, border, col, names.arg, angle, main, sub, xlab, ylab.
Other Parameter you can use according to your requirement in question
Code:-
# Data
values <- c(10, 25, 15, 30)
names <- c("Q1", "Q2", "Q3", "Q4")
# Basic barplot
barplot(values,
names.arg = names,
col = "skyblue",
space=1,
border="red",
angle=20,
density=50,
main = "Revenue by Quarter",
sub= "USD",
xlab = "Quarter",
ylab = "Revenue in USD")
Result:-
Explanation:-
- density=50 means area shaded of bar , if you increase this value then more area shaded
- col= for the color of bar like here skyblue (Don't use capital letter)
- space=1, means bar are place 1-1 space distance away
- angle=20, means 20 degree angle of line in bar as you can see in bar
- main = for the title (Here in this eg. Revenue by Quarter)
- sub= for the subtitle (Here in this eg. USD)
- xlab= use for label the x axis as you can see "Quarter"
- ylab= use for label the y axis as you can see "Revenue in USD"
- For better understanding just copy the code and change the value and try by yourself
You have sales data for 3 different product (X,Y,Z) across four different sales region (North, South, East, West). Create a stacked bar plot to compare total sales per region, while showing the contribution of each product.
Data (filled row by row: X then Y then Z:
salesdata <-c (50,45,60,55, # Product X
30,25,40,35, # Product Y
20,30,25,15) # Product Z
- Organize the data into a matrix using nrow=3 and byrow=TRUE
- Name the rows "Product X", "Product Y", "Product Z".
- Name the column "North", "South", "East", "West"
- Use distinct colors for each product
- Include a legend and main title "Quarterly Product Sales by Region".
Code:-
salesdata <-c (50,45,60,55, # Product X
30,25,40,35, # Product Y
20,30,25,15) # Product Z
# Create a matrix (2 rows for products, 3 columns for regions, filled by row)
salematrix <- matrix (salesdata, nrow = 3, byrow = TRUE)
# Add names for clarity
colnames (salematrix) <- c("North", "South", "East", "West")
rownames (salematrix) <- c("Product X", "Product Y", "Product Z")
# Create the stacked bar plot
barplot (salematrix,
main = "Quaternery sales",
xlab = "Product",
ylab = "Region",
col = rainbow (length (rownames (salematrix))), # Colors for the two products
legend.text=rownames(salematrix), # Automatically adds legend
args.legend=list(x="topright", inset=c(-0.2,-0.4))) #Position the legend box
Solution:-
Explanation:-
- In solution image, you not see first 3 line , in these line data written as given in question (you can see in code)
- In 6th line we have to use matrix, to stacked bar one above another
- nrow=3 means height of bar is 3 (you see in image 3 different color product X,Y,Z)
- byrow=TRUE means if data here:- 50,45,60,55, 30,25,40,35, 20,30,25,15) then it fill the first row then 2nd then 3rd like that --> means 50 in North, 45 in South, 55 in East, 55 in West like that then repeat
- In 9th line you see colnames (these use here to specify the column or row)
- In 10th line you see rownames mainly row denote here height of bar, which you earlier decide 3 so here 3 different name use
- I think you have difficulty in understanding the rownames and colnames, for better understanding just copy the code and try yourself how its work, it gives you better idea.
- Then in 14th line, all parameter which use in barplot - salematrix show the data which we use, col=denote the color which we use, rainbow here denote the rainbow color, which randomly give the color to bar
- legend.text= Use here to create a seperate dialogue box as you can see with written product X,Y,Z with denote color as we earlier mention
- args.legend to place the dialogue box in which position, inset mainly to place at particular position but use "c" before the x,y value
Example-4:- Legend Box
Basically it a seperate box to denote the data of the main chart
In above image, Pink circle denote the legend box, so there are some parameter by which you can change this box design or data.
All parameter of legend()
Position & Layout:-
Content & Symbols:-
Aesthetics & Styling:-
Example-5:- In example 3 you see we put legend command in barplot but their is a different way
Code:-
salesdata <-c (50,45,60,55, # Product X
30,25,40,35, # Product Y
20,30,25,15) # Product Z
# Create a matrix (2 rows for products, 3 columns for regions, filled by row)
salematrix <- matrix (salesdata, nrow = 3, byrow = TRUE)
# Add names for clarity
colnames (salematrix) <- c("North", "South", "East", "West")
rownames (salematrix) <- c("Product X", "Product Y", "Product Z")
# Create the stacked bar plot
barplot (salematrix,
main = "Quaternery sales",
xlab = "Product",
ylab = "Region",
col = rainbow (length (rownames (salematrix)))) # Colors for the two products
# Add the legend box separately
legend(x = "topright", # Position: "topleft", "topright", "center", etc.
legend = rownames(salematrix), # The actual labels for the legend items (MANDATORY)
title = "Product", # Optional title for the legend
col = rainbow(length(rownames(salematrix))), # Colors must match your barplot
pch = 15, # Point character: 15 for a filled square
bty = "o", # Box type: "o" draws a box around it (default)
cex=0.5,
inset=c(-0.01,0.1))
Result:-
Explanation:-
Compare this to example no 3, here in 20th line we add a seperate legend() command, I already explain these thing in code and Result image you can check there. If diffculty in understanding just copy the code from this blog and paste in your Rstudio and see the result difference for better understanding
Example-6:- Another way to use legend box with "fill" command instead of "col"
Code:-
salesdata <-c (50,45,60,55, # Product X
30,25,40,35, # Product Y
20,30,25,15) # Product Z
# Create a matrix (2 rows for products, 3 columns for regions, filled by row)
salematrix <- matrix (salesdata, nrow = 3, byrow = TRUE)
# Add names for clarity
colnames (salematrix) <- c("North", "South", "East", "West")
rownames (salematrix) <- c("Product X", "Product Y", "Product Z")
# Create the stacked bar plot
barplot (salematrix,
main = "Quaternery sales",
xlab = "Product",
ylab = "Region",
col = rainbow (length (rownames (salematrix)))) # Colors for the two products
fillcolor=rainbow(length(rownames(salematrix)))
# Add the legend box separately
legend(x = "topright", # Position: "topleft", "topright", "center", etc.
legend = rownames(salematrix), # The actual labels for the legend items (MANDATORY)
title = "Product", # Optional title for the legend
fill=fillcolor, # Colors must match your barplot
bty = "o", # Box type: "o" draws a box around it (default)
cex=0.5,
inset=c(-0.01,0.1))
Result:-
- If we compare this with previous example no 5, here we use a different command "fill" instead of "col" and also no need of use "pch", because "pch" mainly code for different symbol
- You can use either "fill" or use "pch" and "col" both
- "bty" mainly for the outline of legend box, there are multiple parameter you can use as you can in the table.
Example-7:- What happen if use use only col but not "pch"
Code:-
salesdata <-c (50,45,60,55, # Product X
30,25,40,35, # Product Y
20,30,25,15) # Product Z
# Create a matrix (2 rows for products, 3 columns for regions, filled by row)
salematrix <- matrix (salesdata, nrow = 3, byrow = TRUE)
# Add names for clarity
colnames (salematrix) <- c("North", "South", "East", "West")
rownames (salematrix) <- c("Product X", "Product Y", "Product Z")
# Create the stacked bar plot
barplot (salematrix,
main = "Quaternery sales",
xlab = "Product",
ylab = "Region",
col = rainbow (length (rownames (salematrix)))) # Colors for the two products
# Add the legend box separately
legend(x = "topright", # Position: "topleft", "topright", "center", etc.
legend = rownames(salematrix), # The actual labels for the legend items (MANDATORY)
title = "Product", # Optional title for the legend
col=rainbow(length(rownames(salematrix))), # Colors must match your barplot
bty = "o", # Box type: "o" draws a box around it (default)
cex=0.5,
inset=c(-0.01,0.1))
Result:-
Explanation:- If "pch" not use here as you see in result image , no symbol seen like square but you see in previous example 6 (Mainly "pch" for the different symbol like circle/square)
Example-8:- Compare to previous example no 7 , here we use both "col" and "pch"
Code:-
salesdata <-c (50,45,60,55, # Product X
30,25,40,35, # Product Y
20,30,25,15) # Product Z
# Create a matrix (2 rows for products, 3 columns for regions, filled by row)
salematrix <- matrix (salesdata, nrow = 3, byrow = TRUE)
# Add names for clarity
colnames (salematrix) <- c("North", "South", "East", "West")
rownames (salematrix) <- c("Product X", "Product Y", "Product Z")
# Create the stacked bar plot
barplot (salematrix,
main = "Quaternery sales",
xlab = "Product",
ylab = "Region",
col = rainbow (length (rownames (salematrix)))) # Colors for the two products
# Add the legend box separately
legend(x = "topright", # Position: "topleft", "topright", "center", etc.
legend = rownames(salematrix), # The actual labels for the legend items (MANDATORY)
title = "Product", # Optional title for the legend
col=rainbow(length(rownames(salematrix))), # Colors must match your barplot
bty = "o", # Box type: "o" draws a box around it (default)
cex=0.5,
pch=15,
inset=c(-0.01,0.1))
Result:-
Explanation:- Here we use both col and pch, and you see square with matching color in legend box







