Making Simple Plots in R
Import and read data from files in R
Step 1. We recommend that you work in the same working sub-directory that you created previously, using one of the following optionsBefore launching R$ cd exerciseR
$ pwd
/Users/imac/Desktop/exerciseR
$ R
> setwd("/Users/imac/Desktop/exerciseR")
> getwd()
[1] "/Users/imac/Desktop/exerciseR"
> Iris <- read.table("iris.txt")
> library(datasets)
> data(iris)
Basics of plotting graphics in R
Introduction The principle of plotting using R commands is to provide generally 2 main information types: (1) the data we want to use, and (2) preferences or options for display. This information should be provided as individual elements, that will be interpreted in R as arguments. They are interpreted as layers of information. Simple basic graphics in R Basic graph types found in typical spreadsheet software also exist in R, such as histograms, barplots, scatterplots or boxplots. These can be generated using commands or functions such as “hist()”, “barplot()”, “plot()”, “boxplot()” respectively. Many others exist, all as part of the ‘base’ graphics package of R, but we will only cover examples of graphs generated with “hist()” and “plot()”. For a full list of variants, use> library(help = "graphics")
Making simple basic graphics in R
The structure usage is function(data, options)Whatever is specified after the command is called arguments. Each command or function has its own set of arguments, but they will all follow the same structure.
Please note that:
- Some plotting functions in R can be used with either a whole data set, or specific data from a data frame (such as “plot()”), but others need data to be specified (such as “hist()”)
- you can also provide your data with no options. This will generate an automatic graph of the data and will use the default options of the command.
> example("hist")
> hist(iris$Sepal.Length)

The following options will rename the x-axis (xlab), give a title to the graph (main), color the borders (border), color the bars (col), and modify the y-axis limits (ylim)
> hist(iris$Sepal.Length, xlab="Sepal Length",main="Histogram of Sepal Length", border="white",col="red3", ylim=c(1, 40))

> hist(iris$Sepal.Length, xlab="Sepal Length",main="Histogram of Sepal Length", border=FALSE,col="#CD0000", ylim=c(1, 40))

> methods(plot)
> plot(iris)

> plot(iris$Sepal.Length, iris$Petal.Length)
> plot(Petal.Length ~ Sepal.Length, data=iris)
> plot(Petal.Length ~ Sepal.Length, iris)
> with(iris, plot(Sepal.Length, Petal.Length))

> plot(iris$Sepal.Length, iris$Petal.Length,main="Sepal vs Petal Lengths", xlab="Sepal.Length",ylab="Petal.Length", pch="*", cex=2.0, col="red3")

> par(bg="lightgrey", mai=c(2,1,2,1.5))
> plot(iris$Sepal.Length, iris$Petal.Length,main="Sepal vs Petal Lengths", xlab="Sepal.Length",ylab="Petal.Length", pch="*", cex=3.0, col="red3")

Saving a plot
By default, any plot you generate will be displayed in your graphic device window. To save a plot, you will have different options. Option 1. First choose the output format (such as jpeg, png, pdf…), name your plot, generate it, then escape by closing the file. You will find the saved file in your working directory. To make and save a file using default options> pdf('test_hist.pdf')
> hist(iris$Sepal.Length)
> dev.off()
> pdf('test_hist.pdf', 7, 10)
> hist(iris$Sepal.Length)
> dev.off()
> hist2(iris$Sepal.Length)
> dev.copy(pdf,'test_hist2.pdf')
> dev.off()
Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education