Customizing the chart

Add title to the chart.

R




plot_ly(data, labels = ~category, values = ~value, type = "pie",
        hole = 0.5, marker = marker)%>%
   
  # customize the layout of the chart, including the title and legend
  layout(title = "Donut Chart Example")


 

The layout function is used to add a title to the chart, with the text “Donut Chart Example”. This improves the chart by adding context and clarity to what the chart is showing.

 

Add a label and text info

R




# define the text and hover information to be displayed on the chart
textinfo <- "label+percent"
hoverinfo <- "text"
...
# create the pie chart with a hole in the center
plot_ly(data, labels = ~category, values = ~value, type = "pie",
        hole = 0.5, marker = marker,
        text = ~paste(category, value, sep = ": "),
        textinfo = textinfo, hoverinfo = hoverinfo)


  ...

To improve the chart by providing more information and context for the data presented and making it easier for the viewer to interpret and understand the chart. The text parameter is added to display the label and value of each category on the chart, separated by a colon (:). The textinfo parameter is also defined as “label+percent” to show both the label and the percentage of each category in the chart.

Additionally, the hoverinfo parameter is set to “text” to display the text defined in text when the user hovers over each category in the chart.

 

Donut chart thickness

R




plot_ly(data, labels = ~category, values = ~value, type = "pie",
        hole = 0.8, marker = marker)%>%
   
  # customize the layout of the chart, including the title and legend
  layout(title = "Donut Chart Example")


We can maintain the thickness of donut chart by increase or decrease the value in hole.

 

Adding Custom color Palettes 

R




# define the colors for each category
colors <- c("#2ecc71", "#3498db", "#f1c40f", "#e74c3c", "#9b59b6")
 
# set the marker properties, including the colors and line width
marker <- list(colors = colors)


The colors of the pie chart are changed to a custom color palette. Specifically, the hex codes for five colors (green, blue, yellow, red, and purple) are defined and stored in the colors vector. These colors are then passed to the marker parameter to set the colors for each category in the pie chart.

This change improves the chart by using a custom color scheme that is more visually appealing and/or aligned with a particular branding or design style.

 

Customizing legend

R




colors <- c("#2ecc71", "#3498db", "#f1c40f", "#e74c3c", "#9b59b6")
 
# set the marker properties, including the colors and line width
marker <- list(colors = colors)
 
# define the text and hover information to be displayed on the chart
textinfo <- "label+percent"
hoverinfo <- "text"
 
# create the pie chart with a hole in the center
chart <- plot_ly(data, labels = ~category, values = ~value, type = "pie",
                 hole = 0.5, marker = marker,
                 text = ~paste(category, value, sep = ": "),
                 textinfo = textinfo, hoverinfo = hoverinfo)
 
# customize the layout of the chart, including the title and legend
layout(chart, title = "Donut Chart Example",
       legend = list(x = 0, y = 1, font = list(size = 14, color = "black")))


Added a legend customization to the plot using the layout function. The purpose of this change is to adjust the position and font of the legend to make it more readable and aesthetically pleasing. 

 

Adding a Pie Slice Border

R




chart <- plot_ly(data, labels = ~category, values = ~value, type = "pie",
                 hole = 0.5, marker = marker,
                 text = ~paste(category, value, sep = ": "),
                 textinfo = textinfo, hoverinfo = hoverinfo)
 
# customize the layout of the chart, including the title and legend
layout(chart, title = "Donut Chart Example",
       legend = list(x = 0, y = 1, font = list(width = 2, color = "blue")))


Added the “line” parameter to the “marker” list with values specifying the color and width of the marker line. This adds a line border around each pie slice. 

 

Donut Chart in R

Donut charts are a type of circular chart like pie charts but with hole in the middle. They’re great way for showing data in aeasy-to-see way. If you’re using R, there are loads of packages to make donut charts – think ggplot2, plotly, webr, and others. 

 Donut charts are used to show data in a circular manner . Since all of the data points are centered on the donut’s rim, it is simple to compare various data points. Additionally, since the size of the slice corresponds to the amount of the data. donut charts can be used to evaluate proportions. Donut charts are a flexible and effective method to show data in general way . 

Similar Reads

Concepts:

If we want to make a donut chart using R Programming, there are three main things we need to know. Firstly, we need the data we want to show on the chart, saved in a way that makes plotting easy like a vector or a data frame. Next, we have to select a package to make the chart with – each package’s got their own way of making and personalizing charts. And lastly, we need to figure out the various parts of the chart like the colors, labels, and slices, and how to tweak them to provide the full story we want to tell....

Steps:

Below is the basic steps outlined to create donut chart in R:...

Example 1: Simple pie and a donut graph together on one plot using ggplot2

The first thing we do is to create a sample dataset that includes the categories and values that we want to plot:...

Example 2: Pie-Donut Chart for Sample Browser Market Share using webr.

...

Customizing the chart

...

Example 3: Donut Chart using plotly package

...

Customizing the chart

...

Conclusion:

...

Contact Us