Customization of the box plot
Add some color and other features in the box plot we use several inbuilt functions.
R
library (ggplot2) # Set a custom theme theme_set ( theme_minimal ()) # Modify the appearance of the plot ggplot (diamonds, aes (x = cut, y = price)) + geom_violin (fill = "#66C2A5" , color = "#084B83" , alpha = 0.8) + geom_boxplot (width = 0.2, fill = "#EFCB68" , color = "#8F2D56" , alpha = 0.8) + # Customize axes labels and title labs (x = "Cut" , y = "Price" , title = "Violin Plot with Box Plot" ) + # Adjust axis text size and color theme (axis.text = element_text (size = 12, color = "#333333" )) + # Add a background color to the plot area theme (plot.background = element_rect (fill = "#F7F7F7" )) + # Modify the legend appearance theme (legend.background = element_rect (fill = "#F7F7F7" ), legend.position = "bottom" ) + # Adjust the plot margins theme (plot.margin = margin (20, 20, 20, 20)) |
Output:
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.
Contact Us