Posts

Showing posts with the label ggplot2

Jittering in R (ggplot2)

Image
Jittering is the act of adding random noise to data in order to prevent overplotting in statistical graphs. Overplotting can occur when a continuous measurement is rounded to a convenient unit. This has the effect of making a continuous variable appear like a discrete ordinal variable.  For example, age is measured in years and body weight is measured in pounds or kilograms.  A scatter plot of weight versus age, which includes a sufficiently large sample of people will involve considerable overlap. Many individuals may be recorded as, 29 years old and weighing 70 kg, and there will be many markers plotted at the point (29, 70). The same is often true when plotting other individual difference metrics throughout psychology (e.g. personality) (Figure 1). Figure 1: Before Jittering - a significant positive correlation between x & y [r = .37]. To alleviate overplotting, it is possible to add a small amount of random noise to the data...

Arranging Multiple Plots in R

Image
Arranging several plots in R as part of a grid isn't always straightforward. Imagine you had three plots and wanted one to stretch along the bottom row and place the other two above (i.e. Figure 1). Figure 1: Sketch of intended placement Using grid.arrange , it is possible to plot several outputs, but the function will automatically place them into a default grid structure (Figure 2). grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, p9) Figure 2: Using grid.arrange This may be perfectly adequate if they are to be of equal size. However, this isn't always ideal. For example, when aligning the plots below, the result becomes cluttered (Figure 3). grid.arrange(p3, p4, p5) Figure 3: Work in progress As per my original sketch, I want the top plot (with no legend) to run along the bottom row and the other two plots to be positioned side by side above. The following example shows how this can be accomplished. First, a new layout is created, here a...