flowchart LR
A{Which variables?} --> B{Data properties}
B --> C{Figure type}
IOC-R Week 5
&, |, !, return TRUE or FALSE.==, !=, <, <=, >, >=.%in%: check membershipWhat does the following code do?
Packages are collections of functions, data, and documentation.
base}, {utils}, {graphics}, etc.To check the list of installed packages in RStudio:

By default, R will install the lastest version of a package.
install.packages("ggplot2")

Or use remove.packages("tibble").
We’ll talk about packages’ version management via the renv package in session 6 if time allowed.
To use (call) a function from a package, we can either:
A loaded package will be checked in the “Packages” panel.
You only need to load a package once per R session.
However, if you’re running your script in a non-interactive way, make sure to include the library() calls in your script, ideally at the beginning.
pkg_name::fct_nameThis way is recommanded if you need to use only one function of a package.
What message you want to show via your figure?
flowchart LR
A{Which variables?} --> B{Data properties}
B --> C{Figure type}
Check out these websites: from Data to Viz and The R Graph Gallery (by Yan Holtz)
(Figure adpated from QCBS R Workshop Series.)
All ggplot2 plots begin with a call to
ggplot(), supplying default data and aesthethic mappings, specified byaes(). You Then add layers, scales, coords and facets with+. —— ggplot2 Reference
Example using the built-in dataset iris:
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Start by calling ggplot():
Specify data, x and y axes.
The data should be a data frame containing both variables needed for the plot.
The most common aesthetics: color, fill, shape, size, alpha (transparency), etc.
The most common aesthetics: color, fill, shape, size, alpha (transparency), etc.
The most common aesthetics: color, fill, shape, size, alpha (transparency), etc.
The most common aesthetics: color, fill, shape, size, alpha (transparency), etc.
Each geom_*() function adds a new layer to the plot, just like stacking transparent sheets on top of each other to build the final image.
geom_histogram() and geom_bar() only require one variable for the x-axis. The y-axis is automatically calculated.
theme_*()): theme_grey() (default),theme_bw(), theme_light(), theme_classic(), etc.
Use ggsave() to save plots in high resolution for publications.
ggsave(
filename = "path/to/figure.png", # figure file name
plot = last_plot(),
# save by default the last figure,
# you can provide the figure name to specify the plot to be saved.
device = "png",
# can be one of "eps", "ps", "tex" (pictex), "pdf",
# "jpeg", "tiff", "png", "bmp", "svg" or "wmf"
width = 6.3,
height = 4.7,
units = "in", # can be one of "in", "cm", "mm" or "px"
dpi = 300 # plot resolution
)Save the basic plot to the outputs folder in your project. Check the saved figure via the Files panel in RStudio.
