I happened to have lots of plots to do with slight variations, so I made a function to make my life easier.

**This function, in particular, serves to compare 2 or more
experimental groups (although it will still plot with 1 group).**
We use violin plots instead of bar plots because they provide more
information for the same space (precisely, the distribution
density/frequency of responses, which allows you to visually estimate
normality, skewness, and kurtosis).

Let’s first load the demo data. This data set comes with base
`R`

(meaning you have it too and can directly type this
command into your `R`

console).

```
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
```

Load the `rempsyc`

package:

If you haven’t installed this package yet, you will need to install it via the following command:Note:`install.packages("rempsyc")`

. Furthermore, you may be asked to install the following packages if you haven’t installed them already (you may decide to install them all now to avoid interrupting your workflow if you wish to follow this tutorial from beginning to end):

```
Dots = means
Error bars = 95% confidence intervals
Width = distribution density (frequency)
```

```
ggplot2::ggsave("nice_violinplothere.pdf",
width = 7, height = 7,
unit = "in", dpi = 300
)
# Change the path to where you would like to save it.
# If you copy-paste your path name,
# remember to use "R" slashes ('/' rather than '\').
# Also remember to specify the .tiff extension of the file.
```

Pro tip: Recommended dimensions for saving is 7 inches wide and 7 inches high. The`.eps`

formats are recommended for scalable vector graphics for high-resolution submissions to scientific journals. However, you can also save in other formats, such as`.tiff`

,`.png`

, or`.jpg`

.

To see if two groups are statistically significantly different.

You can also select groups based on their position on the x-axis (notice no quotes this time).

What if you want to look at all three groups at the same time?
Unfortunately, the underlying package we use, `ggsignif`

,
does not allow the comparison of more than one group at once. So we need
to tweak this manually instead. (Note that we can also use this
technique when the significance computed with `ggsignif`

does
not correspond to the number of stars we want to use, for example if we
use a different *p*-value or not a *t*-test.)

```
nice_violin(
data = ToothGrowth,
group = "dose",
response = "len",
signif_annotation = c("*", "**", "***"), # manually enter the number of stars
signif_yposition = c(30, 36, 40), # What height (y) should the stars appear?
signif_xmin = c(1, 2, 1), # Where should the left-sided brackets start (x)?
signif_xmax = c(2, 3, 3)
) # Where should the right-sided brackets end (x)?
```