You want to visualize the proportion of traffic your website receives from five different sources. Create a pie chart that include the percentage value on each slice.
Data (Number of visitors): trafficcounts <- c(1200, 800, 450, 300, 250)
Sources: sources <- c("Organic Search", "Direct", "Referral", "Social Media", "Email")
Calculate the percentage for each source
Labels on the slices should show the calculated percentage (eg. 40%)
Include a main title "Website Traffic Sources"
Use a rainbow colour palette
Add a legend showing the source names in the top right corner
Code:-
trafficcounts <- c(1200, 800, 450, 300, 250)
sources <- c("Organic Search", "Direct", "Referral", "Social Media", "Email")
percent <- round(100*trafficcounts/sum(trafficcounts), 1) #calculation of percentage
percentlabels <- paste(sources, "(", percent, "%", ")") # Add % sign
# Define a color palette
colorpalette <- rainbow(length(sources))
# Create the pie chart with percentage labels
pie(trafficcounts, labels = percentlabels, main = "Website Traffic Sources",
col = colorpalette)
# Add a legend to the top right
legend("topleft", sources,
cex = 0.5, # Adjust legend size
fill = colorpalette, inset=c(0,0))
Solution:-
Explanation:-
- In the 1st and 2nd line we simply take the data which given in example
- We have to use c to compile the data
- In 3rd line, first we have to calculate the percentage of each sources, and you here use "round" to round off the data and in last you see 1 means round off to 1 digit
- In 5th line, we use here to label each pie , here paste0 to combine all the data with 0 gap, if we only write here "paste" then you see gap in the label - Direct(26.7%), Organic Search(40%)
- In 5th line you see paste0(sources, "(", percent, "%" , ")") --> means it combine all the data first it take data from sources (which we already mention like organic search, direct etc.), after this you see bracket open and then you see percent (means you calculate in 3rd line) ,after that it add % sign and bracket close sign
- In the 11th line here we use all the parameter like labels to label the pie chart, col use for colours
- In the 15th line legend use for label the pie in a seperate box , cex use to for adjust the font in the box, inset to place the box in particular position, fill use to show the color in box
All parameter of pie
Example-2:-
data <- c (600,300,150,100,200)
labels <-c ("Housing", "Food", "Clothes", "Entertainment", "Other")
--> Draw a 3d pie chart with label the each slice with percentage
Code:-
# Define data and labels
data <- c (600, 300, 150, 100, 200)
labels <- c("Housing", "Food", "Clothes", "Entertainment", "Other")
#Calculate percentages
percentage <- round(100 * data / sum(data), 1)
#label the pie slice
labelbox <- paste(labels, "-", percentage, "%", sep='')
# You can either use paste0 or paste with sep=' ' to reduce the gap
# Create the 3D Pie Chart
pie3D(data,
labels = labelbox,
explode = 0.1, # Separates slices slightly
main = "Monthly Expenditure", # Title of the chart
col = rainbow(length(data)), # Vibrant color palette
labelcex = 0.8) # Adjust label text size
Solution:-
Explanation:-
- In 2nd and 3rd line you see the data as given in example
- In 6th line we first calculate the percentage of each pie slice
- In 9th line we label the complete name which we want to mention in each slice of pie, we use here paste so we combine the name with percentage and here use sep=" if we reduce the gap between the data or which we can use either "paste0"
- In 13th line we use all the parameter of pie3d (for run pie3d download plotrix from library)
- "explode" use to seperate slice slightly
- "labelcex" use to resize the font size
All parameter of pie3D
Label Customization:-
Layout and Ordering:-


No comments:
Post a Comment