# How to Draw a plotly Violin Plot in R (Example)

Hello again folks! Welcome to another interesting plotly tutorial. In this one, you will be introduced to the violin plot and will learn how to make one in plotly using the R programming language.

Before we start, however, let us get a sneak preview of what this tutorial is going to teach us:

Having seen what we are going to learn in this tutorial, let’s dive into it!

## What is a Violin Plot

A violin plot is type of plot that is typically used to visualize the distribution of numeric data and compare groups within a data frame. It can be used to view different descriptive statistics about the data, such as the min, max, median, first quartile, and third quartile. It shares some similarities with the box plot in that like the box plot, it is used to view the distribution of numeric data, but it reports more information than the box plot by showing the entire distribution of the numeric data, including its density.

## Requirements to Draw a plotly Violin Plot

Having been introduced to the violin plot and how it is used, we are going to build a basic one in plotly using the R programming language. First, we will need to install the plotly library. Therefore, in R Studio, or in your preferred code editor that can run the R environment, run the line of code below:

`install.packages("plotly")`

The line of code above will install the plotly library in your R programming environment. Let us now load the library and all its dependencies. In your IDE, please run the code below to load the plotly library:

`library(plotly)`

This will load the plotly library and all its plot-building functions, including the one we will use to build our violin plot.

Furthermore, we will need to install and load the R dplyr library, which we will need to be able to pipe lines of code together into a chunk. So, run the lines of code below to install and load the dplyr library, just as we had done for plotly:

`install.packages("dplyr")`
`library(dplyr)`

## How to Draw a plotly Violin Plot

Now that we have installed and loaded both the plotly and dplyr libraries, we can now go on to build a violin plot. I have created a random data frame for this part of the tutorial, and you can replicate it in your project as well by running the lines of code below:

```set.seed(1234)
fruits = floor(runif(20,2,40)),
flowers = floor(runif(20,5,50))
)```

In the code above, running `set.seed(1234)` before building the data frame that follows ensures that our result is reproducible at all times.

You can view the first 10 rows of the data frame by running:

`basket |> head(10)`
```#     fruits flowers
#1       2      44
#2      16      36
#3      30      36
#4       7      47
#5      39      26
#6      23      34
#7      19      28
#8      33      12
#9      11      36
#10      5      17```

We can now draw a violin plot of the “flowers” column to visually analyze its distribution and other statistics. Run the code below to draw a violin plot:

```fig <- basket |>
plot_ly(y = ~flowers,
type = "violin",
meanline = list(visible = T),
x0 = "Flowers") |>
layout(yaxis = list(zeroline = F,
title = ""))

fig```

In the code chunk above, we specified that we want the mean line to be visible so that we can easily see the average count of the flower column. Furthermore, we specified that we do not want the zero line to be visible, which would have drawn a line at the base of our plot where the Y axis is 0.

If you hover over the plot, you will see different statistics about the numeric data, including its kernel density score, which provide important information about the distribution of the data points.

## Combine a Violin Plot and a Box Plot

Having drawn a basic violin plot, let us see how to combine it with a box plot. We only need to define one argument for our violin plot to be combined with a box plot as shown in the code below:

```fig <- basket |>
plot_ly(y = ~flowers,
type = "violin",
meanline = list(visible = T),
box = list(visible = T),
x0 = "Flowers") |>
layout(yaxis = list(zeroline = F,
title = ""))

fig```

In the code chunk above, we only needed to define the argument `box = list(visible = T)` to add a box plot to our violin plot. And by default, a box plot draws the median line of the numeric data, which is the thick line in the box.

With this, we can see a more comprehensive analysis of the distribution of the numeric data in the flower column of the basket data frame. The combination of a violin plot and a box plot is, therefore, inherently more powerful than when the plots are separate. And because the graph is interactive, a user can quickly learn about the data by hovering their mouse over the plot and seeing summary statistics.

So, that is how to draw a violin plot in plotly using the R programming language. You can try to draw violin plots for other kinds of numeric data and see what information you will get from your plot.

I hope you enjoyed this tutorial, and I will see you soon in the next one!

## Video, Further Resources & Summary

Do you need more explanations on how to draw a violin plot in plotly using R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to draw a violin plot in plotly using the R programming language.

Furthermore, you could have a look at some other tutorials on Statistics Globe:

This post has shown how to draw a violin plot in plotly using the R programming language. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

Subscribe to the Statistics Globe Newsletter