# How to create basic plots (in R)

## Task

Plotting is a huge topic with many options and variations, but the most foundational types of plots are a line plot and a scatterplot. How can we create those?

Related topics:

- How to add details to a plot
- How to create a histogram
- How to create a box (and whisker) plot
- How to change axes, ticks, and scale in a plot
- How to create bivariate plots to compare groups
- How to plot interaction effects of treatments

## Solution

We will create some fake data using vectors, for simplicity. But everything we show below works also if your data is in columns of a DataFrame.

1
2
3

patient.id <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
patient.height <- c(60, 64, 64, 65, 66, 66, 70, 72, 72, 76)
patient.weight <- c(141, 182, 169, 204, 138, 198, 180, 175, 244, 196)

We can make a line plot if we use the `type="l"`

option (which is an “ell,” not a number one).

1

plot(patient.id, patient.height, main="Patient heights", type="l")

We can create a scatterplot if we have two numerical columns, such as the height and weight in the data above.

1

plot(patient.height, patient.weight, main = "Height vs. Weight")

Content last modified on 24 July 2023.

See a problem? Tell us or edit the source.

Contributed by:

- Elizabeth Czarniak (CZARNIA_ELIZ@bentley.edu)
- Nathan Carter (ncarter@bentley.edu)