\(~\)

After the niceViolin() function, here’s how to make nice scatter plots easily!

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!).

data("mtcars")
head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Source the function from my github:

source("https://raw.githubusercontent.com/RemPsyc/niceplots/master/niceScatterFunction.R")

\(~\)

Make the basic plot

Save a high-resolution image file to specified directory

Pro tip: Change .tiff for .pdf or .eps for scalable vector graphics for high-resolution submissions to scientific journals!

\(~\)

Change x- and y- axis labels

Have points “jittered”

Meaning randomly moved around a bit to prevent overplotting (when two or more points overlap, thus hiding information).

Change the transparency of the points

Remove points

Add confidence band

Set x- and y- scales manually

Change plot color

Plot by group

Use full range on the slope/confidence band

Add a legend

Change order of labels on the legend

Change legend labels

**Warning**: This only changes labels and applies after changing order of level!
Always use `groups.order` first if you also need to use `groups.names`!
This is to make sure to have the right labels for the right groups!

Add a title to legend

Plot by group + manually specify colours

Plot by group + use different line types for each group

Plot by group + use different point shapes for each group

Plot by group, point shapes, line types, legend + no colours (black and white)

Putting it all together

If you’d like to see all available options at once (a bit long):

Special situation: Add group average

There’s no straightforward way to add group average, so here’s a hack to do it. We first have to create a second data set with another “group” that will be used as the average.

Then we need to create a FIRST layer of just the slopes. We add transparency to the group lines except the group average to emphasize the group average (with the new argument manual.slope.alpha).

Finally we are ready to add a SECOND layer of just the points on top of our previous layer. We use standard ggplot syntax for this.

If you’d like instead to still show the group points but only the black average line, you can do the following as first layer:

Then to add the points as second layer we do the same as before:

\(~\)

\(~\)

Concluding Statement

Make sure to check out this page again if you use the code after a time or if you encounter errors, as I periodically update or improve the code.

You can always edit the function to suit your purposes, or contact me for questions or requests to modify this function at remitheriault.wixsite.com/site/contact! Thanks for reading my guide! :) \(~\)

\(~\)

\(~\)

\(~\)

\(~\)


Updated 2020-05-29

\(~\)

\(~\)

\(~\)

\(~\)

\(~\)

\(~\)