Mean marker customization
In ggplot2, we use the stat_summary() function to compute new summary statistics and add them to the plot. We use the stat_summary() function with ggplot() function.
Syntax:
plot+ stat_summary(fun.y, geom, size, color)
Here,
- fun.y: determines the function according to which marker has to be placed i.e. mean, median, etc.
- geom: determines the shape of marker
- size: determines size of marker
- color: determines the color of marker
Example:
In this example, we will compute the mean value of the y-axis variable using fun.y argument in the stat_summary() function.
R
# load library ggplot2 library (ggplot2) # Basic violin plot # diamonds dataframe has been used here # diamonds dataframe is provided by R # language natively. ggplot (diamonds, aes (x=cut, y=price)) + # geom_violin() function is used to plow violin plot geom_violin ()+ # Stat_summary() function adds mean marker on plot stat_summary (fun.y= "mean" , geom= "point" , size=2, color= "red" ) |
Output:
Here, the point in the center of the violin shows the variation of the mean of the y-axis for each category of data on the x-axis.
How To Make Violin Plots with ggplot2 in R?
Violin plots help us to visualize numerical variables from one or more categories. They are similar to box plots in the way they show a numerical distribution using five summary-level statistics. But violin plots also have the density information of the numerical variables. It allows visualizing the distribution of several categories by displaying their densities.
In this article, we will discuss how to plot a violin plot with the help of the ggplot2 library in R Programming Language. To plot a violin plot using the ggplot2 package we use the geom_violin() function.
Syntax: ggplot( dataframe, aes( x, y, fill, color)) + geom_violin()
Parameters:
- dataframe: determines the dataset used in the plot.
- fill: determines the color of background of interior of the plot.
- color: determines the color of boundary of plot.