Â

Â

Â

Â

Cite as: Alfonso Garmendia (2019) R for life sciences. Chapter 3: R base graphics and data management. http://personales.upv.es/algarsal/R-tutorials/03_Tutorial-3_R-base-graphics.html

available also in other formats (pdf, docx, â€¦): https://drive.google.com/drive/folders/19w914WCg8BVTVBE_zpgShmg2vpjguV1e?usp=sharing

Originals in bitbucket repository: https://bitbucket.org/alfonsogar/tea_daa_tutorials

Â

Â

Â

Â

Written in Rmarkdown, using Rstudio.

# Basic graphics and data

## Basic graphics with one variable

One of the main reasons to use R instead of statistical programs is for its strong graphical capabilities. To see some of these capabilities, write demo(graphics).

Basic graph types are density plots, dot plots, bar charts, line charts, pie charts, box-plots and scatter plots.

Plots in R have two types of commands, high-level commands to create the plot and low-level commands to add things to the plot, once it has been created. These low-level commands will do nothing if there is not an active plot.

Some of the most used low-level commands are:

points () :
lines() :
abline () :
title() :
legend() :
text() :
Add a text string at the desired coordinates into a figure.

The main primary command is plot(). Depending the input data, it will do the type of plot that best fit. But of course is possible to change. Looking at the help(plot) page is very advisable before start and take a look into the arguments. Changing for example the type of plot.

``````################ BASIC GRAPHS ###################
# Define the greenfly vector with 5 values
greenfly <- c(1, 3, 6, 4, 9)

################ POINTS #########################
# Graph the greenfly vector with all defaults
plot(greenfly)``````

``````################ LINES  #########################
# Graph the greenfly vector with a line
plot(greenfly, type = "l")``````

``````################ BARPLOT ########################
# Names for the bars
years <- as.character(1999:2003)
# Graph the greenfly vector with barplot
barplot(greenfly, names.arg = years)``````

``````################ PIE PLOT #######################
# Graph the greenfly vector with a line
pie(greenfly, labels = years)``````

``````################ BOX PLOT #######################
# Graph the greenfly vector with a boxplot
boxplot(greenfly)``````