Skip to content

Commit

Permalink
fix package dependencies, plot workaround for Rstudio + Binder
Browse files Browse the repository at this point in the history
  • Loading branch information
ha0ye committed Aug 28, 2018
1 parent dceae76 commit fd89d35
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 97 deletions.
5 changes: 4 additions & 1 deletion install.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ install.packages("lavaan")
install.packages("plyr")
install.packages("cowplot")
install.packages("rmarkdown")
install.packages("readr")
install.packages("readr")
install.packages("caTools")
install.packages("bitops")

43 changes: 39 additions & 4 deletions tutorial_R/raincloud_tutorial_r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ knitr::opts_chunk$set(fig.width=6, fig.height=3, fig.path='figs/',
echo=FALSE, warning=FALSE, message=FALSE)
```

## Package dependencies

Make sure we have the packages we need, and install them if they are missing.

```{r package setup, include = TRUE, echo = TRUE}
packages <- c("ggplot2", "dplyr", "lavaan", "plyr", "cowplot", "rmarkdown",
"readr", "caTools", "bitops")
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
```

## How to make it rain
This tutorial will walk you through the process of transforming your barplots into rainclouds, and also show you how to customize your rainclouds for various options such as ordinal or repeated measures data.

Expand Down Expand Up @@ -50,7 +63,9 @@ p1 <- ggplot(summary_simdat, aes(x = group, y = score_mean, fill = group))+
ggtitle("Figure 1: Barplot +/- SEM")
ggsave('../figs/tutorial_R/1Barplot.png', width = w, height = h)
```

```{r figure 1}
p1
```

Expand All @@ -65,7 +80,9 @@ p2 <- ggplot(simdat,aes(x=group,y=score))+
ggtitle('Figure 2: Basic Rainclouds or Little Prince Plot')
ggsave('../figs/tutorial_R/2basic.png', width = w, height = h)
```

```{r figure 2}
p2
```

Expand All @@ -80,7 +97,9 @@ p3 <- ggplot(simdat,aes(x=group,y=score, fill = group))+
ggtitle('Figure 3: The Basic Raincloud with Colour')
ggsave('../figs/tutorial_R/3pretty.png', width = w, height = h)
```

```{r figure 3}
p3
```

Expand All @@ -95,7 +114,9 @@ p4 <- ggplot(simdat,aes(x=group,y=score, fill = group))+
ggtitle('Figure 4: Unsmooth Rainclouds')
ggsave('../figs/tutorial_R/4unsmooth.png', width = w, height = h)
```

```{r figure 4}
p4
```

Expand All @@ -112,7 +133,9 @@ p5 <- ggplot(simdat,aes(x=group,y=score, fill = group))+
ggtitle("Figure 5: Raincloud Plot w/Boxplots")
ggsave('../figs/tutorial_R/5boxplots.png', width = w, height = h)
```

```{r figure 5}
p5
```

Expand All @@ -130,11 +153,11 @@ p6 <- ggplot(simdat,aes(x=group,y=score, fill = group, colour = group))+
ggtitle("Figure 6: Change in Colour Palette")
ggsave('../figs/tutorial_R/6boxplots.png', width = w, height = h)
p6
```


```{r figure 6}
p6
```

Alternatively, you may prefer to simply plot mean or median with standard confidence intervals. Here we'll plot the mean as well as 95% confidence intervals, which we've calculated using the included SummarySE function, by overlaying them on of our clouds:

Expand All @@ -151,7 +174,9 @@ p7 <- ggplot(simdat,aes(x=group,y=score, fill = group, colour = group))+
ggtitle("Figure 7: Raincloud Plot with Mean ± 95% CI")
ggsave('../figs/tutorial_R/7meanplot.png', width = w, height = h)
```

```{r figure 7}
p7
```

Expand Down Expand Up @@ -192,6 +217,9 @@ title <- ggdraw() +
all_plot_final <- plot_grid(title, all_plot, ncol = 1, rel_heights = c(0.1, 1)) # rel_heights values control title margins
ggsave('../figs/tutorial_R/8allplot.png', width = w, height = h)
```

```{r figure 8}
all_plot_final
```

Expand All @@ -212,7 +240,9 @@ p9 <- ggplot(simdat,aes(x=group,y=score, fill = group, colour = group))+
ggtitle("Figure 9: Complex Raincloud Plots with Facet Wrap")
ggsave('../figs/tutorial_R/9facetplot.png', width = w, height = h)
```

```{r figure 9}
p9
```

Expand All @@ -230,7 +260,6 @@ sumrepdat <- summarySE(rep_data, measurevar = "score", groupvars=c("group", "tim
head(sumrepdat)
```

Now, we'll plot our rainclouds with boxplots again, this time adding some dodge so we can better emphasize differences between our factors and factor levels. Note that here we need to nudge the point x-axis as a numeric valuable, as this work around does not currently work for boxplots with multiple factors:
Expand All @@ -246,7 +275,9 @@ p10 <- ggplot(rep_data, aes(x = time, y = score, fill = group)) +
ggtitle("Figure 10: Repeated Measures Factorial Rainclouds")
ggsave('../figs/tutorial_R/10repanvplot.png', width = w, height = h)
```

```{r figure 10}
p10
```

Expand All @@ -267,7 +298,9 @@ p11 <- ggplot(rep_data, aes(x = time, y = score, fill = group)) +
ggtitle("Figure 11: Repeated Measures - Factorial (Extended)")
ggsave('../figs/tutorial_R/11repanvplot2.png', width = w, height = h)
```

```{r figure 11}
p11
```

Expand All @@ -289,7 +322,9 @@ p12 <- ggplot(rep_data, aes(x = group, y = score, fill = time)) +
coord_flip()
ggsave('../figs/tutorial_R/12repanvplot3.png', width = w, height = h)
```

```{r figure 12}
p12
```

Expand Down
282 changes: 227 additions & 55 deletions tutorial_R/raincloud_tutorial_r.html

Large diffs are not rendered by default.

63 changes: 26 additions & 37 deletions tutorial_R/raincloud_tutorial_r.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ output:



## Package dependencies

Make sure we have the packages we need, and install them if they are missing.


```r
packages <- c("ggplot2", "dplyr", "lavaan", "plyr", "cowplot", "rmarkdown",
"readr", "caTools", "bitops")

if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
```

## How to make it rain
This tutorial will walk you through the process of transforming your barplots into rainclouds, and also show you how to customize your rainclouds for various options such as ordinal or repeated measures data.

Expand Down Expand Up @@ -55,11 +69,9 @@ p1 <- ggplot(summary_simdat, aes(x = group, y = score_mean, fill = group))+
ggtitle("Figure 1: Barplot +/- SEM")

ggsave('../figs/tutorial_R/1Barplot.png', width = w, height = h)

p1
```

![](figs/barplot-1.png)<!-- -->
![](figs/figure 1-1.png)<!-- -->

There we go - just needs some little asterixes and we're ready to publish! Just kidding. Let's start our first, most basic raincloud plot like so, using the 'geom_flat_violin' option our function already setup for us:

Expand All @@ -73,11 +85,9 @@ p2 <- ggplot(simdat,aes(x=group,y=score))+
ggtitle('Figure 2: Basic Rainclouds or Little Prince Plot')

ggsave('../figs/tutorial_R/2basic.png', width = w, height = h)

p2
```

![](figs/basic_rc-1.png)<!-- -->
![](figs/figure 2-1.png)<!-- -->

Now we can see the raw data (our 'rain'), and the overlaid probability distribution (the 'cloud'). Let's make it a bit prettier and easier to read by adding some colours. We can also use 'coordinate flip' to rotate the entire plot about the x-axis, transforming our 'little prince plots' into true rainclouds:

Expand All @@ -91,11 +101,9 @@ p3 <- ggplot(simdat,aes(x=group,y=score, fill = group))+
ggtitle('Figure 3: The Basic Raincloud with Colour')

ggsave('../figs/tutorial_R/3pretty.png', width = w, height = h)

p3
```

![](figs/pretty_rc-1.png)<!-- -->
![](figs/figure 3-1.png)<!-- -->

In case you want to change the smoothing kernel used to calculate the PDFs, you can do so by altering the 'adjust' flag for geom_flat_violin. For example, here we've dropped our smoothing to give a much bumpier raincloud:

Expand All @@ -109,11 +117,9 @@ p4 <- ggplot(simdat,aes(x=group,y=score, fill = group))+
ggtitle('Figure 4: Unsmooth Rainclouds')

ggsave('../figs/tutorial_R/4unsmooth.png', width = w, height = h)

p4
```

![](figs/unsmooth_rc-1.png)<!-- -->
![](figs/figure 4-1.png)<!-- -->

Now we need to add something to help us easily evaluate any possible differences between our groups or conditions. To achieve this, we'll add some boxplots to complete our raincloud plots. To get the boxplots to line up however we like, we need to set our x-axis to a numeric value, so we can add a fixed offset:

Expand All @@ -129,11 +135,9 @@ p5 <- ggplot(simdat,aes(x=group,y=score, fill = group))+
ggtitle("Figure 5: Raincloud Plot w/Boxplots")

ggsave('../figs/tutorial_R/5boxplots.png', width = w, height = h)

p5
```

![](figs/boxplot_rc-1.png)<!-- -->
![](figs/figure 5-1.png)<!-- -->

Now we'll make a few aesthetic tweaks. You may want to turn these on or off depending on your preferences. We'll take the black outline away from the plots by adding the colour = group parameter, and we'll also change colour palettes using the built-in colour brewer tool.

Expand All @@ -150,13 +154,9 @@ p6 <- ggplot(simdat,aes(x=group,y=score, fill = group, colour = group))+
ggtitle("Figure 6: Change in Colour Palette")

ggsave('../figs/tutorial_R/6boxplots.png', width = w, height = h)

p6
```

![](figs/colour_rc-1.png)<!-- -->


![](figs/figure 6-1.png)<!-- -->

Alternatively, you may prefer to simply plot mean or median with standard confidence intervals. Here we'll plot the mean as well as 95% confidence intervals, which we've calculated using the included SummarySE function, by overlaying them on of our clouds:

Expand All @@ -174,11 +174,9 @@ p7 <- ggplot(simdat,aes(x=group,y=score, fill = group, colour = group))+
ggtitle("Figure 7: Raincloud Plot with Mean ± 95% CI")

ggsave('../figs/tutorial_R/7meanplot.png', width = w, height = h)

p7
```

![](figs/meanplot_rc-1.png)<!-- -->
![](figs/figure 7-1.png)<!-- -->

If your data is discrete or ordinal you may need to manually add some jitter to improve the plot:

Expand Down Expand Up @@ -218,10 +216,9 @@ title <- ggdraw() +
all_plot_final <- plot_grid(title, all_plot, ncol = 1, rel_heights = c(0.1, 1)) # rel_heights values control title margins

ggsave('../figs/tutorial_R/8allplot.png', width = w, height = h)
all_plot_final
```

![](figs/striated-1.png)<!-- -->
![](figs/figure 8-1.png)<!-- -->

Finally, in many situations you may have nested, factorial, or repeated measures data. In this case, one option is to use plot facets to group by factor, emphasizing pairwise differences between conditions or factor levels:

Expand All @@ -241,11 +238,9 @@ p9 <- ggplot(simdat,aes(x=group,y=score, fill = group, colour = group))+
ggtitle("Figure 9: Complex Raincloud Plots with Facet Wrap")

ggsave('../figs/tutorial_R/9facetplot.png', width = w, height = h)

p9
```

![](figs/factorial-1.png)<!-- -->
![](figs/figure 9-1.png)<!-- -->

As another example, we consider some simulated repeated measures data in factorial design, where two groups are measured across three timepoints. To do so, we'll first load in some new data:

Expand Down Expand Up @@ -288,11 +283,9 @@ p10 <- ggplot(rep_data, aes(x = time, y = score, fill = group)) +
ggtitle("Figure 10: Repeated Measures Factorial Rainclouds")

ggsave('../figs/tutorial_R/10repanvplot.png', width = w, height = h)

p10
```

![](figs/repdata2-1.png)<!-- -->
![](figs/figure 10-1.png)<!-- -->

Finally, you may want to add traditional line plots to emphasize factorial interactions and main effects. Here we've plotted the mean and standard error for each cell of our design, and connected these with a hashed line. There are a lot of possible options though, so you'll need to decide what works best for your needs:

Expand All @@ -312,11 +305,9 @@ p11 <- ggplot(rep_data, aes(x = time, y = score, fill = group)) +
ggtitle("Figure 11: Repeated Measures - Factorial (Extended)")

ggsave('../figs/tutorial_R/11repanvplot2.png', width = w, height = h)

p11
```

![](figs/repdata3-1.png)<!-- -->
![](figs/figure 11-1.png)<!-- -->

Here is the same plot, but with the grouping variable flipped:

Expand All @@ -337,11 +328,9 @@ p12 <- ggplot(rep_data, aes(x = group, y = score, fill = time)) +
coord_flip()

ggsave('../figs/tutorial_R/12repanvplot3.png', width = w, height = h)

p12
```

![](figs/repdata4-1.png)<!-- -->
![](figs/figure 12-1.png)<!-- -->


That's it! We hope you'll be able to use this tutorial to find great illustrations for your data, and that we've given you an idea of some of the different ways you can customize your raincloud plots.

0 comments on commit fd89d35

Please sign in to comment.