Example 3: Donut Chart using plotly package

The plotly package is an R Programming package that provides an interface to create interactive web-based visualization using the Plotly JawaScript library.

R




library(plotly)
 
# create a data frame with category labels and corresponding values
data <- data.frame(
  category = c("A", "B", "C", "D", "E"),
  value = c(20, 25, 15, 10, 30)
)
 
# define the colors for each category
colors <- c("red", "orange", "yellow", "green", "blue")
 
# set the marker properties, including the colors and line width
marker <- list(colors = colors)
 
# create the pie chart with a hole in the center
plot_ly(data, labels = ~category, values = ~value, type = "pie",
        hole = 0.5, marker = marker)


First, a data frame is created with five categories labeled A through E, and their corresponding values. Then, a vector of colors is defined for each category.

Next, the marker variable is defined to include the colors for each category and any other marker properties such as line width.

Finally, the plot_ly function is used to create the pie chart, with the data data frame as the input, the category column as the labels, the value column as the values, and type = “pie” to specify that it is a pie chart. The hole parameter specifies the size of the hole in the center of the chart, and marker = marker applies the marker properties defined earlier.

 

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