{"id":8317,"date":"2018-12-30T14:56:19","date_gmt":"2018-12-30T12:56:19","guid":{"rendered":"https:\/\/www.datanovia.com\/en\/?post_type=dt_lessons&#038;p=8317"},"modified":"2019-11-17T22:02:43","modified_gmt":"2019-11-17T20:02:43","slug":"ggplot-violin-plot","status":"publish","type":"dt_lessons","link":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/","title":{"rendered":"GGPlot Violin Plot"},"content":{"rendered":"<div id=\"rdoc\">\n<p>A <strong>Violin Plot<\/strong> is used to visualize the distribution of the data and its probability density.<\/p>\n<p>This chart is a combination of a Box plot and a Density Plot that is rotated and placed on each side, to display the distribution shape of the data.<\/p>\n<p>Typically, violin plots will include a marker for the median of the data and a box indicating the interquartile range, as in standard boxplots.<\/p>\n<p>A Violin Plot shows more information than a Box Plot. For example, in a violin plot, you can see whether the distribution of the data is bimodal or multimodal.<\/p>\n<p>This article describes how to create and customize <strong>violin plots<\/strong> using the <strong>ggplot2<\/strong> R package.<\/p>\n<p>Contents:<\/p>\n<div id=\"TOC\">\n<ul>\n<li><a href=\"#key-r-functions\">Key R functions<\/a><\/li>\n<li><a href=\"#data-preparation\">Data preparation<\/a><\/li>\n<li><a href=\"#loading-required-r-package\">Loading required R package<\/a><\/li>\n<li><a href=\"#basic-violin-plots\">Basic violin plots<\/a><\/li>\n<li><a href=\"#create-a-violin-plot-with-multiple-groups\">Create a Violin Plot with multiple groups<\/a><\/li>\n<li><a href=\"#conclusion\">Conclusion<\/a><\/li>\n<\/ul>\n<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div class='dt-sc-ico-content type1'><div class='custom-icon' ><a href='https:\/\/www.datanovia.com\/en\/product\/ggplot2-essentials-for-great-data-visualization-in-r\/' target='_blank'><span class='fa fa-book'><\/span><\/a><\/div><h4><a href='https:\/\/www.datanovia.com\/en\/product\/ggplot2-essentials-for-great-data-visualization-in-r\/' target='_blank'> Related Book <\/a><\/h4>GGPlot2 Essentials for Great Data Visualization in R<\/div>\n<div class='dt-sc-hr-invisible-medium  '><\/div>\n<div id=\"key-r-functions\" class=\"section level2\">\n<h2>Key R functions<\/h2>\n<p>Key function:<\/p>\n<ul>\n<li><code>geom_violin()<\/code>: Creates violin plots. Key arguments:\n<ul>\n<li><code>color<\/code>, <code>size<\/code>, <code>linetype<\/code>: Border line color, size and type<\/li>\n<li><code>fill<\/code>: Areas fill color<\/li>\n<li><code>trim<\/code>: logical value. If TRUE (default), trim the tails of the violins to the range of the data. If FALSE, don\u2019t trim the tails.<\/li>\n<\/ul>\n<\/li>\n<li><code>stat_summary()<\/code>: Adds summary statistics (mean, median, \u2026) on the violin plots.<\/li>\n<\/ul>\n<\/div>\n<div id=\"data-preparation\" class=\"section level2\">\n<h2>Data preparation<\/h2>\n<ul>\n<li>Demo dataset: <code>ToothGrowth<\/code>\n<ul>\n<li>Continuous variable: <code>len<\/code> (tooth length). Used on y-axis<\/li>\n<li>Grouping variable: <code>dose<\/code> (dose levels of vitamin C: 0.5, 1, and 2 mg\/day). Used on x-axis.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>First, convert the variable <code>dose<\/code> from a numeric to a discrete factor variable:<\/p>\n<pre class=\"r\"><code>data(\"ToothGrowth\")\r\nToothGrowth$dose &lt;- as.factor(ToothGrowth$dose)\r\nhead(ToothGrowth, 4)<\/code><\/pre>\n<pre><code>##    len supp dose\r\n## 1  4.2   VC  0.5\r\n## 2 11.5   VC  0.5\r\n## 3  7.3   VC  0.5\r\n## 4  5.8   VC  0.5<\/code><\/pre>\n<\/div>\n<div id=\"loading-required-r-package\" class=\"section level2\">\n<h2>Loading required R package<\/h2>\n<p>Load the ggplot2 package and set the default theme to <code>theme_classic()<\/code> with the legend at the top of the plot:<\/p>\n<pre class=\"r\"><code>library(ggplot2)\r\ntheme_set(\r\n  theme_classic() +\r\n    theme(legend.position = \"top\")\r\n  )<\/code><\/pre>\n<\/div>\n<div id=\"basic-violin-plots\" class=\"section level2\">\n<h2>Basic violin plots<\/h2>\n<p>We start by initiating a plot named <code>e<\/code>, then we\u2019ll add layers. The following R code creates Violin Plots combined with summary statistics (mean +\/- SD) and Box Plots.<\/p>\n<p>Create basic violin plots with summary statistics:<\/p>\n<pre class=\"r\"><code># Initiate a ggplot\r\ne &lt;- ggplot(ToothGrowth, aes(x = dose, y = len))\r\n\r\n# Add mean points +\/- SD\r\n# Use geom = \"pointrange\" or geom = \"crossbar\"\r\ne + geom_violin(trim = FALSE) + \r\n  stat_summary(\r\n    fun.data = \"mean_sdl\",  fun.args = list(mult = 1), \r\n    geom = \"pointrange\", color = \"black\"\r\n    )\r\n    \r\n# Combine with box plot to add median and quartiles\r\n# Change fill color by groups, remove legend\r\ne + geom_violin(aes(fill = dose), trim = FALSE) + \r\n  geom_boxplot(width = 0.2)+\r\n  scale_fill_manual(values = c(\"#00AFBB\", \"#E7B800\", \"#FC4E07\"))+\r\n  theme(legend.position = \"none\")<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggplot2\/figures\/006-ggplot-violin-plot-geom_violin-violin-plot-with-summary-statistics-1.png\" width=\"268.8\" \/><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggplot2\/figures\/006-ggplot-violin-plot-geom_violin-violin-plot-with-summary-statistics-2.png\" width=\"268.8\" \/><\/p>\n<div class=\"notice\">\n<p>The function <code>mean_sdl<\/code> is used for adding mean and standard deviation. It computes the mean plus or minus a constant times the standard deviation. In the R code above, the constant is specified using the argument <code>mult<\/code> (mult = 1). By default mult = 2. The mean +\/- SD can be added as a crossbar or a pointrange.<\/p>\n<\/div>\n<\/div>\n<div id=\"create-a-violin-plot-with-multiple-groups\" class=\"section level2\">\n<h2>Create a Violin Plot with multiple groups<\/h2>\n<p>Two different grouping variables are used: <code>dose<\/code> on x-axis and <code>supp<\/code> as line color (legend variable).<\/p>\n<p>The space between the grouped plots is adjusted using the function <code>position_dodge()<\/code>.<\/p>\n<pre class=\"r\"><code>e + geom_violin(aes(color = supp), trim = FALSE, position = position_dodge(0.9) ) +\r\n  geom_boxplot(aes(color = supp), width = 0.15, position = position_dodge(0.9)) +\r\n  scale_color_manual(values = c(\"#00AFBB\", \"#E7B800\"))<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/dn-tutorials\/ggplot2\/figures\/006-ggplot-violin-plot-geom_violin-violin-plot-with-multiple-groups-1.png\" width=\"336\" \/><\/p>\n<\/div>\n<div id=\"conclusion\" class=\"section level2\">\n<h2>Conclusion<\/h2>\n<p>This article describes how to create a Violin Plot using the ggplot2 package.<\/p>\n<\/div>\n<\/div>\n<p><!--end rdoc--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A Violin Plot is used to visualize the distribution of the data and its probability density. This chart is a combination of a Box plot and a Density Plot that is rotated and placed on each side, to display the distribution shape of the data. A Violin Plot shows more information than a Box Plot. For example, in a violin plot, you can see whether the distribution of the data is bimodal or multimodal. This article describes how to create and customize violin plots using the ggplot2 R package.<\/p>\n","protected":false},"author":1,"featured_media":9133,"parent":0,"menu_order":6,"comment_status":"open","ping_status":"closed","template":"","class_list":["post-8317","dt_lessons","type-dt_lessons","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>GGPlot Violin Plot - Datanovia<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"GGPlot Violin Plot - Datanovia\" \/>\n<meta property=\"og:description\" content=\"A Violin Plot is used to visualize the distribution of the data and its probability density. This chart is a combination of a Box plot and a Density Plot that is rotated and placed on each side, to display the distribution shape of the data. A Violin Plot shows more information than a Box Plot. For example, in a violin plot, you can see whether the distribution of the data is bimodal or multimodal. This article describes how to create and customize violin plots using the ggplot2 R package.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/\" \/>\n<meta property=\"og:site_name\" content=\"Datanovia\" \/>\n<meta property=\"article:modified_time\" content=\"2019-11-17T20:02:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/\",\"name\":\"GGPlot Violin Plot - Datanovia\",\"isPartOf\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg\",\"datePublished\":\"2018-12-30T12:56:19+00:00\",\"dateModified\":\"2019-11-17T20:02:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#primaryimage\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg\",\"width\":1024,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.datanovia.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Lessons\",\"item\":\"https:\/\/www.datanovia.com\/en\/lessons\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"GGPlot Violin Plot\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#website\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"name\":\"Datanovia\",\"description\":\"Data Mining and Statistics for Decision Support\",\"publisher\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.datanovia.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#organization\",\"name\":\"Datanovia\",\"url\":\"https:\/\/www.datanovia.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"contentUrl\":\"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png\",\"width\":98,\"height\":99,\"caption\":\"Datanovia\"},\"image\":{\"@id\":\"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"GGPlot Violin Plot - Datanovia","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/","og_locale":"en_US","og_type":"article","og_title":"GGPlot Violin Plot - Datanovia","og_description":"A Violin Plot is used to visualize the distribution of the data and its probability density. This chart is a combination of a Box plot and a Density Plot that is rotated and placed on each side, to display the distribution shape of the data. A Violin Plot shows more information than a Box Plot. For example, in a violin plot, you can see whether the distribution of the data is bimodal or multimodal. This article describes how to create and customize violin plots using the ggplot2 R package.","og_url":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/","og_site_name":"Datanovia","article_modified_time":"2019-11-17T20:02:43+00:00","og_image":[{"width":1024,"height":512,"url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/","url":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/","name":"GGPlot Violin Plot - Datanovia","isPartOf":{"@id":"https:\/\/www.datanovia.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#primaryimage"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#primaryimage"},"thumbnailUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg","datePublished":"2018-12-30T12:56:19+00:00","dateModified":"2019-11-17T20:02:43+00:00","breadcrumb":{"@id":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#primaryimage","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2019\/05\/P1040339.JPG.jpg","width":1024,"height":512},{"@type":"BreadcrumbList","@id":"https:\/\/www.datanovia.com\/en\/lessons\/ggplot-violin-plot\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.datanovia.com\/en\/"},{"@type":"ListItem","position":2,"name":"Lessons","item":"https:\/\/www.datanovia.com\/en\/lessons\/"},{"@type":"ListItem","position":3,"name":"GGPlot Violin Plot"}]},{"@type":"WebSite","@id":"https:\/\/www.datanovia.com\/en\/#website","url":"https:\/\/www.datanovia.com\/en\/","name":"Datanovia","description":"Data Mining and Statistics for Decision Support","publisher":{"@id":"https:\/\/www.datanovia.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.datanovia.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.datanovia.com\/en\/#organization","name":"Datanovia","url":"https:\/\/www.datanovia.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","contentUrl":"https:\/\/www.datanovia.com\/en\/wp-content\/uploads\/2018\/09\/datanovia-logo.png","width":98,"height":99,"caption":"Datanovia"},"image":{"@id":"https:\/\/www.datanovia.com\/en\/#\/schema\/logo\/image\/"}}]}},"multi-rating":{"mr_rating_results":[]},"_links":{"self":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/8317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons"}],"about":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/types\/dt_lessons"}],"author":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/comments?post=8317"}],"version-history":[{"count":0,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/dt_lessons\/8317\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media\/9133"}],"wp:attachment":[{"href":"https:\/\/www.datanovia.com\/en\/wp-json\/wp\/v2\/media?parent=8317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}