Previously, we described the essentials of R programming and provided quick start guides for importing data into **R**.

This chapter contains articles describring how to visualize data using **R base graphs**.

- Creating graphs
- Saving graphs
- File formats for exporting plots

Read more: —> Creating and Saving Graphs in R.

Read more: —> Generic plot types in R.

- R base scatter plot: plot()
- Enhanced scatter plots: car::scatterplot()
- 3D scatter plots

Read more: —> Scatter Plots.

- R base scatter plot matrices: pairs()
- Use the R package psych

Read more —> Scatter Plot Matrices.

- R base box plots: boxplot()
- Box plot with the number of observations: gplots::boxplot2()

Read more —> Box Plots.

Read more —> Strip Charts: 1-D scatter Plots.

- Basic bar plots
- Change group names
- Change color
- Change main title and axis labels

- Stacked bar plots
- Grouped bar plots

Read more —> Bar Plots.

- R base functions: plot() and lines()
- Basic line plots
- Plots with multiple lines

Read more —> Line Plots.

- Create basic pie charts: pie()
- Create 3D pie charts: plotix::pie3D()

Read more —> Pie Charts.

- Create histogram plots: hist()
- Create density plots: density()

`## [1] 48.96467 56.38715 60.42221 43.27151 57.14562 57.53028`

Read more —> Histogram and Density Plots.

Use **qqPlot**() [in **car** package]:

Read more —> QQ-plots: Quantile-Quantile plots.

- R base function: dotchart()
- Dot chart of one numeric vector
- Dot chart of a matrix

Read more —> Dot charts.

Read more —> Plot Group Means and Confidence Intervals.

- Add and customize titles
- Add legends
- Add texts
- Add straight lines
- Add an axis to a plot
- Change axis scale : minimum, maximum and log scale
- Customize tick mark labels
- Change plotting symbols
- Change line types
- Change colors

Read more —> R base graphical Parameters.

This analysis has been performed using **R statistical software** (ver. 3.2.4).

Previously, we described the essentials of R programming and provided quick start guides for importing data into **R**.

Here, we’ll describe how to create **quantile-quantile** plots in R. **QQ plot** (or quantile-quantile plot) draws the correlation between a given sample and the normal distribution. A 45-degree reference line is also plotted. QQ plots are used to visually check the normality of the data.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the built-in R data set named ToothGrowth.

```
# Store the data in the variable my_data
my_data <- ToothGrowth
```

The R base functions **qqnorm**() and **qqplot**() can be used to produce quantile-quantile plots:

**qqnorm**(): produces a normal QQ plot of the variable**qqline**(): adds a reference line

```
qqnorm(my_data$len, pch = 1, frame = FALSE)
qqline(my_data$len, col = "steelblue", lwd = 2)
```

It’s also possible to use the function **qqPlot**() [in **car** package]:

```
library("car")
qqPlot(my_data$len)
```

As all the points fall approximately along this reference line, we can assume normality.

This analysis has been performed using **R statistical software** (ver. 3.2.4).

Previously, we described the essentials of R programming and provided quick start guides for importing data into **R**.

Here, we’ll describe how to create **mean plots** with **confidence intervals** in R.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in ToothGrowth data set.

`head(ToothGrowth)`

```
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
```

The function **plotmeans**() [in **gplots** package] can be used.

```
library(gplots)
# Plot the mean of teeth length by dose groups
plotmeans(len ~ dose, data = ToothGrowth, frame = FALSE)
```

```
# Add mean labels (mean.labels = TRUE)
# Remove line connection (connect = FALSE)
plotmeans(len ~ dose, data = ToothGrowth, frame = FALSE,
mean.labels = TRUE, connect = FALSE)
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to draw a Cleveland **dot plot** in R.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in mtcars data set.

We’ll use *mtcars* data sets. We start by ordering the data set according to mpg variable.

`mtcars <- mtcars[order(mtcars$mpg), ]`

The function **dotchart**() is used to draw a cleveland dot plot.

```
dotchart(x, labels = NULL, groups = NULL,
gcolor = par("fg"), color = par("fg"))
```

**x**: numeric vector or matrix**labels**: a vector of labels for each point.**groups**: a grouping variable indicating how the elements of x are grouped.**gcolor**: color to be used for group labels and values.**color**: the color(s) to be used for points and labels.

```
# Dot chart of a single numeric vector
dotchart(mtcars$mpg, labels = row.names(mtcars),
cex = 0.6, xlab = "mpg")
```

```
# Plot and color by groups cyl
grps <- as.factor(mtcars$cyl)
my_cols <- c("#999999", "#E69F00", "#56B4E9")
dotchart(mtcars$mpg, labels = row.names(mtcars),
groups = grps, gcolor = my_cols,
color = my_cols[grps],
cex = 0.6, pch = 19, xlab = "mpg")
```

```
dotchart(VADeaths, cex = 0.6,
main = "Death Rates in Virginia - 1940")
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to create **histogram** and **density plots** in R.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

The data set contains the value of weight by sex for 200 individuals.

```
set.seed(1234)
x <- c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5))
head(x)
```

`## [1] 48.96467 56.38715 60.42221 43.27151 57.14562 57.53028`

- A histogram can be created using the function
**hist**(), which simplified format is as follow:

`hist(x, breaks = "Sturges")`

**x**: a numeric vector**breaks**: breakpoints between histogram cells.

**Create histograms**

`hist(x, col = "steelblue", frame = FALSE)`

```
# Change the number of breaks
hist(x, col = "steelblue", frame = FALSE,
breaks = 30)
```

The function **density**() is used to estimate kernel density.

```
# Compute the density data
dens <- density(mtcars$mpg)
# plot density
plot(dens, frame = FALSE, col = "steelblue",
main = "Density plot of mpg")
```

```
# Fill the density plot using polygon()
plot(dens, frame = FALSE, col = "steelblue",
main = "Density plot of mpg")
polygon(dens, col = "steelblue")
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to create **pie charts** in R. The R base function **pie**() can be used for this.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

```
df <- data.frame(
group = c("Male", "Female", "Child"),
value = c(25, 25, 50)
)
df
```

```
## group value
## 1 Male 25
## 2 Female 25
## 3 Child 50
```

The function **pie**() can be used to draw a **pie chart**.

`pie(x, labels = names(x), radius = 0.8)`

**x**: a vector of non-negative numerical quantities. The values in x are displayed as the areas of pie slices.**labels**: character strings giving names for the slices.**radius**: radius of the pie circle. If the character strings labeling the slices are long it may be necessary to use a smaller radius.

`pie(df$value, labels = df$group, radius = 1)`

```
# Change colors
pie(df$value, labels = df$group, radius = 1,
col = c("#999999", "#E69F00", "#56B4E9"))
```

Te function **pie3D**()[in **plotrix** package] can be used to draw a 3D pie chart.

**Install** plotrix package:

`install.packages("plotrix")`

Use **pie3D**():

```
# 3D pie chart
library("plotrix")
pie3D(df$value, labels = df$group, radius = 1.5,
col = c("#999999", "#E69F00", "#56B4E9"))
```

```
# Explode the pie chart
pie3D(df$value, labels = df$group, radius = 1.5,
col = c("#999999", "#E69F00", "#56B4E9"),
explode = 0.1)
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to create **line plots** in R. The function **plot**() or **lines**() can be used to create a line plot.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

The simplified format of **plot**() and **lines**() is as follow.

```
plot(x, y, type = "l", lty = 1)
lines(x, y, type = "l", lty = 1)
```

**x, y**: coordinate vectors of points to join**type**: character indicating the type of plotting. Allowed values are:- “p” for points
- “l” for lines
- “b” for both points and lines
- “c” for empty points joined by lines
- “o” for overplotted points and lines
- “s” and “S” for stair steps
- “n” does not produce any points or lines

**lty**: line types. Line types can either be specified as an integer (0=blank, 1=solid (default), 2=dashed, 3=dotted, 4=dotdash, 5=longdash, 6=twodash) or as one of the character strings “blank”, “solid”, “dashed”, “dotted”, “dotdash”, “longdash”, or “twodash”, where “blank” uses ‘invisible lines’ (i.e., does not draw them).

```
# Create some variables
x <- 1:10
y1 <- x*x
y2 <- 2*y1
```

We’ll plot a plot with two lines: **lines**(x, y1) and **lines**(x, y2).

Note that the function **lines**() can not produce a plot on its own. However, it can be used to add **lines**() on an existing graph. This means that, first you have to use the function **plot**() to create an empty graph and then use the function **lines**() to add lines.

```
# Create a basic stair steps plot
plot(x, y1, type = "S")
# Show both points and line
plot(x, y1, type = "b", pch = 19,
col = "red", xlab = "x", ylab = "y")
```

```
# Create a first line
plot(x, y1, type = "b", frame = FALSE, pch = 19,
col = "red", xlab = "x", ylab = "y")
# Add a second line
lines(x, y2, pch = 18, col = "blue", type = "b", lty = 2)
# Add a legend to the plot
legend("topleft", legend=c("Line 1", "Line 2"),
col=c("red", "blue"), lty = 1:2, cex=0.8)
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to create **bar plots** in R. The function **barplot**() can be used to create a **bar plot** with vertical or horizontal bars.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in VADeaths data set.

Data set: *VADeaths*

```
# Data set
VADeaths
```

```
## Rural Male Rural Female Urban Male Urban Female
## 50-54 11.7 8.7 15.4 8.4
## 55-59 18.1 11.7 24.3 13.6
## 60-64 26.9 20.3 37.0 19.3
## 65-69 41.0 30.9 54.6 35.1
## 70-74 66.0 54.3 71.1 50.0
```

```
# Subset
x <- VADeaths[1:3, "Rural Male"]
x
```

```
## 50-54 55-59 60-64
## 11.7 18.1 26.9
```

```
# Bar plot of one variable
barplot(x)
# Horizontal bar plot
barplot(x, horiz = TRUE)
```

`barplot(x, names.arg = c("A", "B", "C"))`

```
# Change border and fill color using one single color
barplot(x, col = "white", border = "steelblue")
# Change the color of border.
# Use different colors for each group
barplot(x, col = "white",
border = c("#999999", "#E69F00", "#56B4E9"))
# Change fill color : single color
barplot(x, col = "steelblue")
# Change fill color: multiple colors
barplot(x, col = c("#999999", "#E69F00", "#56B4E9"))
```

```
# Change axis titles
# Change color (col = "gray") and remove frame
barplot(x, main = "Death Rates in Virginia",
xlab = "Age", ylab = "Rate")
```

```
barplot(VADeaths,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths))
```

```
barplot(VADeaths,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), beside = TRUE)
```

It’s also possible to add legends to a plot using the function **legend**() as follow.

```
# Define a set of colors
my_colors <- c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk")
# Bar plot
barplot(VADeaths, col = my_colors, beside = TRUE)
# Add legend
legend("topleft", legend = rownames(VADeaths),
fill = my_colors, box.lty = 0, cex = 0.8)
```

**box.lty = 0**: Remove the box around the legend**cex = 0.8**: legend text size

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to create **strip charts** (i.e., one dimensional scatter plots or dot plots) in R. These plots are a good alternative to box plots when sample sizes are small.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in ToothGrowth data set.

```
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
# Print the first 6 rows
head(ToothGrowth, 6)
```

```
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
```

`stripchart(x, data = NULL method = "overplot", jitter = 0.1)`

**x**: the data from which the plots are to be produced. Allowed values are one or a list of numeric vector, each corresponding to a component plot.**data**: a data.frame (or list) from which the variables in x should be taken.**Method**: the method to be used to separate coincident points. Allowed values are one of “overplot”, “jitter” or “stack”.**jitter**: when method = “jitter” is used, jitter gives the amount of jittering applied.

```
# Plot len by dose
stripchart(len ~ dose, data = ToothGrowth,
pch = 19, frame = FALSE)
```

```
# Vertical plot using method = "jitter"
stripchart(len ~ dose, data = ToothGrowth,
pch = 19, frame = FALSE, vertical = TRUE,
method = "jitter")
```

```
# Change point shapes (pch) and colors by groups
# add main title and axis labels
stripchart(len ~ dose, data = ToothGrowth,
frame = FALSE, vertical = TRUE,
method = "jitter", pch = c(21, 18, 16),
col = c("#999999", "#E69F00", "#56B4E9"),
main = "Length by dose", xlab = "Dose", ylab = "Length")
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).

**R**.

Here, we’ll describe how to create **box plots** in R.

**Launch RStudio**as described here: Running RStudio and setting up your working directory**Prepare your data**as described here: Best practices for preparing your data and save it in an external .txt tab or .csv files**Import your data**into**R**as described here: Fast reading of data from txt|csv files into R: readr package.

Here, we’ll use the R built-in ToothGrowth data set.

```
# Print the first 6 rows
head(ToothGrowth, 6)
```

```
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
```

Draw a box plot of teeth length (*len*):

**Basic box plots**

```
# Box plot of one variable
boxplot(ToothGrowth$len)
# Box plots by groups (dose)
# remove frame
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE)
# Horizontal box plots
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
horizontal = TRUE)
# Notched box plots
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
notch = TRUE)
```

Notch is used to compare groups. In the notched boxplot, if two boxes’ notches do not overlap this is “strong evidence” their medians differ (Chambers et al., 1983, p. 62).

**Change group names**

```
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
names = c("D0.5", "D1", "D2"))
```

**Change color**

```
# Change the color of border using one single color
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
border = "steelblue")
# Change the color of border.
# Use different colors for each group
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
border = c("#999999", "#E69F00", "#56B4E9"))
# Change fill color : single color
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
col = "steelblue")
# Change fill color: multiple colors
boxplot(len ~ dose, data = ToothGrowth, frame = FALSE,
col = c("#999999", "#E69F00", "#56B4E9"))
```

**Box plot with multiple groups**

```
boxplot(len ~ supp*dose, data = ToothGrowth,
col = c("white", "steelblue"), frame = FALSE)
```

**Change main title and axis labels**

```
# Change axis titles
# Change color (col = "gray") and remove frame
# Create notched box plot
boxplot(len ~ dose, data = ToothGrowth,
main = "Plot of length by dose",
xlab = "Dose (mg)", ylab = "Length",
col = "lightgray", frame = FALSE)
```

The function **boxplot2**()[in **gplots** package] can be used to create a box plot annotated with the number of observations.

Install **gplots**:

`install.packages("gplots")`

Use **boxplot2**() [in gplots]:

```
library("gplots")
# Box plot with annotation
boxplot2(len ~ dose, data = ToothGrowth,
frame = FALSE)
```

```
# Put the annotation at the top
boxplot2(len ~ dose, data = ToothGrowth,
frame = FALSE, top = TRUE)
```

- Create basic box plots:

`boxplot(len ~ dose, data = ToothGrowth, frame = FALSE)`

- Box plots with number of observations:

```
gplots::boxplot2(len ~ dose, data = ToothGrowth,
frame = FALSE, top = TRUE)
```

This analysis has been performed using **R statistical software** (ver. 3.2.4).