Plotting a fully customized plot with the graphics package
So far, we have seen the most important features of the graphics package and how to use its different options. To conclude, the following code adds custom labels and titles to the previous example:
data(iris) iris$color <- sapply(iris$Species, function(x) switch(as.character(x), setosa = "red", versicolor = "green", virginica = "blue")) plot(iris$Sepal.Length,iris$Petal.Length, col= iris$color, pch = 16,main="Sepal Length/Petal Length dispersion graph", xlab ="Sepal Length", ylab="Petal Length",cex=0.8, ylim=c(0,8)) legend(7.2,3,legend=c("setosa","versicolor","virginica"), pch=16, col=c("red","green","blue"),cex=0.7)
This is the output:
In this example, compared to the previous example, the vertical axis has been slightly extended on both the limits by specifying ylim, the dots have been made smaller (setting cex
to 0.8
), the axes labels have been changed (the xlab
and ylab
arguments) and a title has been added (main argument...