How to Plot Graphs with plot() in R

Mahmut Kamalak
5 min readNov 22, 2021

--

R’s outstanding graphic capabilities are one of the primary reasons data analysts choose it. Creating a Graph explains how to make and save graphs in R. The rest of this section will walk you through the process of creating fundamental scatterplot graph types.

Photo by Isaac Smith on Unsplash

Scatterplots

There is no need to find public data to use graphs, R provides us with many built-in datasets. So, we do not need to find and import data-set from anywhere. For the Scatterplot part, the “iris” dataset is used.

Description of the “iris” dataset: This famous (Fisher’s or Anderson’s) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

Let’s see what kind of data do we have ;

# there is no need to import, just use as “iris” to call the data 
# show first 6 lines of the data with head()
head(iris)
--- Sepal.Length Sepal.Width Petal.Length Petal.Width Species
---1 5.1 3.5 1.4 0.2 setosa
---2 4.9 3.0 1.4 0.2 setosa
---3 4.7 3.2 1.3 0.2 setosa
---4 4.6 3.1 1.5 0.2 setosa
---5 5.0 3.6 1.4 0.2 setosa
---6 5.4 3.9 1.7 0.4 setosa
# show the inside structure of a R object
str(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 ..

In R, there are numerous methods for creating a scatterplot. Plot(x, y) is the fundamental function, where x and y are numeric vectors specifying the (x,y) points to plot. Let’s start to plot something;

# plot iris$Sepal.Length(x-axes) and iris$Sepal.Width values(y-axes)
#with default settings of plot()
plot(x = iris$Sepal.Length, y = iris$Sepal.Width )

The graph above is theoretically absolutely correct, but visually very weak and difficult to understand by anyone else. Let’s make it a little clearer and more beautiful;

# add title and axis-labels
plot(iris$Sepal.Length, iris$Sepal.Width,
xlab=”Length [cm]”, ylab=”Width [cm]”,
main=” Titleiris ”) # title

Default color is black, R comes with 657 predefined colors,you can find other options ->https://bookdown.org/hneth/ds4psy/ds4psy_files/figure-html/apx-color-sample-1.png

Default point type is a circle, you can find other options ->
http://www.sthda.com/sthda/RDoc/figure/graphs/r-plot-pch-symbols-points-in-r.png

# change the color of points (many color options)(col=)
# change style of point to plus sign
plot(iris$Sepal.Length, iris$Sepal.Width,
xlab=”Length [cm]”, ylab=”Width [cm]”,
main=”Titleiris”,
col=”purple”, pch=3)

#these color names are built-in R
colors <- c("salmon", "lightblue", "orange")
colors <- c("salmon", "lightblue", "orange")# make 3 groups regarding to iris$Species with our colors vector
# change type of point with adjusting 'pch'
# lets see more value in the x&y axes(like 1,2,3 -> 1,1.5,2,2.5,3)
plot(iris$Sepal.Length, iris$Sepal.Width,
xlab="Length [cm]", ylab="Width [cm]",
main="Titleiris",
col=colors[iris$Species], pch=16,
las=1)

If you want to disable the values on the x and y axes;

# disable the values on the x and y axes by xaxt=”n” , yaxt=”n”
plot(iris$Sepal.Length, iris$Sepal.Width,
xlab=”Length [cm]”, ylab=”Width [cm]”,
main=”Titleiris”,
col=colors[iris$Species], pch=16,
las=1,
xaxt=”n”,
yaxt=”n”)

Also, we can adjust the values LIMits for X-axis by adding “ xlim=c(3,9)” which means values on the x-axis are shown between these two numbers.

Now, I would like to show a line scatterplot, but the iris dataset is not time-series data, so it would be annoying to show on that. Let's see the line graph on the “precip” dataset which is also built-in.

Description of the “precip” dataset: The average amount of precipitation (rainfall) in inches for each of 70 United States (and Puerto Rico) cities. This is time-series data.

#show 6 values of the dataset
head(precip)
---Mobile Juneau Phoenix Little Rock Los Angeles Sacramento
--- 67.0 54.7 7.0 48.5 14.0 17.2
# "type=l" means line graph, but you can change. For example; from #"l" to "b" means showing both lines and circle types.
These are the other options for the type
#line graph for precip dataset
plot(precip, type=”l”)

Summary

Common plot() arguments:

plot(x, y = NULL, type = “p”, xlim = NULL, ylim = NULL,
log = “”, main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par(“ann”), axes = TRUE, frame.plot = axes,
panel.first = NULL, panel.last = NULL, asp = NA,
xgap.axis = NA, ygap.axis = NA,
…)

--

--