{"id":2491,"date":"2022-06-20T20:58:01","date_gmt":"2022-06-20T20:58:01","guid":{"rendered":"http:\/\/optimumsportsperformance.com\/blog\/?p=2491"},"modified":"2022-06-21T13:00:03","modified_gmt":"2022-06-21T13:00:03","slug":"visualizing-group-changes","status":"publish","type":"post","link":"https:\/\/optimumsportsperformance.com\/blog\/visualizing-group-changes\/","title":{"rendered":"Visualizing Group Changes"},"content":{"rendered":"<p><strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/twitter.com\/_CJMayes\">CJ Mayes<\/a><\/span><\/strong> recently posted some really nice plots for <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/twitter.com\/OSPpatrick\/status\/1538215670160060416?s=20&amp;t=LBdsadOr2mp1fgW_2YqjLA\">visualizing group differences to Twitter<\/a><\/span><\/strong>.<\/p>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-2495\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM-1024x806.png\" alt=\"\" width=\"582\" height=\"458\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM-1024x806.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM-300x236.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM-768x605.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM-624x491.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-21-at-5.59.04-AM.png 1316w\" sizes=\"auto, (max-width: 582px) 100vw, 582px\" \/><\/a><\/p>\n<p>My personal favorite was the bottom right plot, which can be a nice way of visualizing pre and post changes in a study. I believe the original plots were done in Tableau, so I&#8217;ve gone ahead and reproduced that bottom right plot in <strong>R<\/strong>.<\/p>\n<p>You can grab the full code, all in one piece, from my <strong><span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/github.com\/pw2\/R-Tips-Tricks\/blob\/master\/Group%20Changes%20-%20Side%20Density%20Plot.R\">GitHub page<\/a><\/span><\/strong>.<\/p>\n<p><strong>Simulate Some Data<\/strong><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nlibrary(tidyverse)\r\nlibrary(gghalves)\r\ntheme_set(theme_light())\r\n\r\nset.seed(1234)\r\ndat &lt;- tibble( subject = LETTERS&#x5B;1:26], pre = rnorm(n = 26, mean = 10, sd = 3) ) %&gt;%\r\n  mutate(post = pre + rnorm(n = 26, mean = 0, sd = 2))\r\n\r\ndat_long &lt;- dat %&gt;%\r\n  pivot_longer(cols = -subject) %&gt;%\r\n  mutate(name = factor(name, levels = c(&quot;pre&quot;, &quot;post&quot;))) \r\n<\/pre>\n<p><strong>Create the plot<\/strong><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ndat_long %&gt;%\r\n  ggplot(aes(x = name, y = value)) +\r\n  geom_line(aes(group = subject),\r\n            color = &quot;light grey&quot;,\r\n            size = 0.7) +\r\n  geom_point(aes(group = subject,\r\n                 color = name),\r\n             alpha = 0.7,\r\n             size = 2) +\r\n  geom_half_violin(aes(x = name),\r\n                   data = dat_long %&amp;gt;% filter(name == 'pre'),\r\n                   fill = &quot;light grey&quot;,\r\n                   color = &quot;light grey&quot;,\r\n                   side = 'l',\r\n                   alpha = 0.7) +\r\n  geom_half_violin(aes(x = name),\r\n                   data = dat_long %&amp;gt;% filter(name == 'post'),\r\n                   fill = &quot;palegreen&quot;,\r\n                   color = &quot;palegreen&quot;,\r\n                   side = 'r',\r\n                   alpha = 0.7) +\r\n  scale_color_manual(values = c(&quot;pre&quot; = &quot;light grey&quot;, &quot;post&quot; = &quot;palegreen&quot;)) +\r\n  labs(x = &quot;Test Time&quot;,\r\n       y = NULL,\r\n       title = &quot;Changes from Pre to Post&quot;) +\r\n  theme(axis.text = element_text(face = &quot;bold&quot;, size = 12),\r\n        axis.title = element_text(face = &quot;bold&quot;, size = 15),\r\n        plot.title = element_text(size = 18),\r\n        legend.position = &quot;none&quot;)\r\n<\/pre>\n<p><a href=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2492\" src=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM-1024x774.png\" alt=\"\" width=\"625\" height=\"472\" srcset=\"https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM-1024x774.png 1024w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM-300x227.png 300w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM-768x580.png 768w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM-624x471.png 624w, https:\/\/optimumsportsperformance.com\/blog\/wp-content\/uploads\/2022\/06\/Screen-Shot-2022-06-20-at-1.57.02-PM.png 1832w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CJ Mayes recently posted some really nice plots for visualizing group differences to Twitter. My personal favorite was the bottom right plot, which can be a nice way of visualizing pre and post changes in a study. I believe the original plots were done in Tableau, so I&#8217;ve gone ahead and reproduced that bottom right [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,43],"tags":[],"class_list":["post-2491","post","type-post","status-publish","format-standard","hentry","category-r-tips-tricks","category-sports-analytics"],"_links":{"self":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/comments?post=2491"}],"version-history":[{"count":3,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2491\/revisions"}],"predecessor-version":[{"id":2496,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/posts\/2491\/revisions\/2496"}],"wp:attachment":[{"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/media?parent=2491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/categories?post=2491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/optimumsportsperformance.com\/blog\/wp-json\/wp\/v2\/tags?post=2491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}