{"id":1835,"date":"2017-01-23T20:39:47","date_gmt":"2017-01-23T20:39:47","guid":{"rendered":"http:\/\/pcool.dyndns.org:8080\/statsbook\/?page_id=1835"},"modified":"2025-07-01T10:04:40","modified_gmt":"2025-07-01T09:04:40","slug":"violin-plot","status":"publish","type":"page","link":"https:\/\/pcool.dyndns.org\/index.php\/violin-plot\/","title":{"rendered":"Violin Plot"},"content":{"rendered":"\n<p>A violin plot is essentially a <a href=\"https:\/\/pcool.dyndns.org\/index.php\/box-plot\/\" data-type=\"page\" data-id=\"501\">box and whisker plot<\/a> where the distribution of the data is represented by a violin (the distribution curve) rather than a box. Therefore, the violin plot gives more information about the distribution of the data than a box and whisker plot. The plot is created with the ggplot2 package<sup class='sup-ref-note' id='note-zotero-ref-p1835-r1-o1'><a class='sup-ref-note' href='#zotero-ref-p1835-r1'>1<\/a><\/sup> .<\/p>\n\n\n\n<p>Download and open the <a href=\"https:\/\/pcool.dyndns.org:\/wp-content\/data_files\/strip.rda\" target=\"_blank\" rel=\"noreferrer noopener\">strip.rda<\/a> dataset for this example and open it in R . The data set contains the maximum flexion in 20 patients before and after a manipulation under anaesthesia. The data can be shown by:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fc0821\" class=\"has-inline-color\">strip<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0712fd\" class=\"has-inline-color\">\n   Flexion  MUA\n1       94  Pre\n2       95  Pre\n.....\n.....\n39     113 Post\n40      86 Post<\/mark><\/em><\/code><\/pre>\n\n\n\n<p>To create a simple violin plot:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(ggplot2)\nggplot(strip, aes(x = MUA, y = Flexion)) + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_violin() + <\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_x_discrete(limits = c('Pre', 'Post')) +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw()<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-1024x1024.png\" alt=\"\" class=\"wp-image-3871\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-1024x1024.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-768x768.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-1536x1536.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin1-2048x2048.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Of course, it is possible to give the different groups a colour, change the order on the x-axis, remove the legend and add the data points:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><span style=\"color: #ff0000;\"><em>library(ggplot2)<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>ggplot(strip, aes(x = MUA, y = Flexion, color = MUA)) +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_violin() +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>geom_jitter(position = position_jitter(0.2))<\/em><\/span><em><mark style=\"background-color:rgba(0, 0, 0, 0);color:#f30808\" class=\"has-inline-color\"> + <\/mark><\/em>\n<span style=\"color: #ff0000;\"><em>scale_x_discrete(limits = c('Pre', 'Post')) +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>scale_color_manual(values = c('red', 'blue')) +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme_bw() +<\/em><\/span>\n<span style=\"color: #ff0000;\"><em>theme(legend.position = 'none')<\/em><\/span><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-1024x1024.png\" alt=\"\" class=\"wp-image-3876\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-1024x1024.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-768x768.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-1536x1536.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violin2-2048x2048.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>A standard violin plot doesn&#8217;t include a median value and a box representing the interquartile range. However, it is quite straightforward to add a box and whisker plot to the plot:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">library(ggplot2)<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggplot(strip, aes(x = MUA, y = Flexion, color = MUA)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_violin(aes(fill = MUA), color = 'black') + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_boxplot(width = 0.3, colour = 'black') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggtitle('Knee Flexion before and after MUA') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_x_discrete(limits = c('Pre', 'Post')) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_y_continuous(limits = c(70,140), 'Flexion in degrees') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_fill_manual(values = c('gold','tomato')) +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme_bw() +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme(legend.position = 'none') <\/span><\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1-1024x1024.png\" alt=\"\" class=\"wp-image-3881\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1-1024x1024.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1-768x768.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1-1536x1536.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox1.png 1800w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If so desired, the mean can also be added (as a diamond shape), so that maxmimum information about the data can be displayed in one plot; with a visible mean, median and mode(s):<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code><em><span style=\"color: #ff0000;\">library(ggplot2)<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggplot(strip, aes(x = MUA, y = Flexion, color = MUA)) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_violin(aes(fill = MUA), color = 'black') + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">geom_boxplot(width = 0.3, colour = 'black') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">ggtitle('Knee Flexion before and after MUA') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_x_discrete(limits = c('Pre', 'Post')) + <\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_y_continuous(limits = c(70,140), 'Flexion in degrees') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">scale_fill_manual(values = c('gold','tomato')) +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme_bw() +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">theme(legend.position = 'none') +<\/span><\/em>\n<em><span style=\"color: #ff0000;\">stat_summary(fun.y = mean, geom = 'point', shape = 23, size = 4, color = 'black')<\/span> <\/em><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1024\" src=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2-1024x1024.png\" alt=\"\" class=\"wp-image-3886\" srcset=\"https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2-1024x1024.png 1024w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2-300x300.png 300w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2-150x150.png 150w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2-768x768.png 768w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2-1536x1536.png 1536w, https:\/\/pcool.dyndns.org\/wp-content\/uploads\/2025\/06\/violinbox2.png 1800w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>A violin plot is essentially a box and whisker plot where the distribution of the data is represented by a violin (the distribution curve) rather than a box. Therefore, the violin plot gives more information about the distribution of the data than a box and whisker plot. The plot is created with the ggplot2 package [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-1835","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/comments?post=1835"}],"version-history":[{"count":4,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1835\/revisions"}],"predecessor-version":[{"id":4675,"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/pages\/1835\/revisions\/4675"}],"wp:attachment":[{"href":"https:\/\/pcool.dyndns.org\/index.php\/wp-json\/wp\/v2\/media?parent=1835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}