Graphics parameters exercises

March 24, 2016

(This article was first published on R-exercises, and kindly contributed to R-bloggers)

plot exercises-2In the exercises below we practice how to personalize graphics parameters, how to produce different plots at the same time and how to save a plot in a file. We will use commands such as par and jpeg. We will use the mtcars dataset, provided by R Cran (we can upload the dataset by typing mtcars and then attach our dataset by attach(mtcars)). A description of the data is available here.

This set is the third set of exercises is a series on data visualization. Click here for the other sets in the series. Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Exercise 1
We are bored to see classic plots and modify every time colors and other details. So, we want to set by default a blue background colour and red dots/lines. What command should we use?


Exercise 2

Suppose we want to see more than one plot at time. What command do we have to use to see 2 plots by row?


Exercise 3

Now we have a general vision to how set and personalize graphics tools in R. In this exercise we have to produce 4 different plots at the same time, 2 by rows and 2 by columns. In addition we have to add some details as: dots colour, labels, titles and an overall legend.

Exercise 4

After exercise 3, we want to turn back to default settings of our machine. How we can reset our settings with one command?

Exercise 5

Plots in exercise 3 seemed to be too much close each other. The solution is set margins of our background plot panel. How can we accomplish this?

a.par(mar=c(4, 4, 4, 4))
b.par(tck=c(1, 2, 2, 1))

Exercise 6

RStudio gives us the opportunity to personalize theme and panels. But we are lazy and we want to set only console and Source panel gave them all available space. How we can plot in other window?;plot(mpg,cyl);plot(mpg,cyl)

Exercise 7

Now, we know well how to set graphic parameters and plot details. So we don’t want to waste our time seeing the plot in a panel, and instead we want to save directly to a jpeg file. What do we have to do?

a.ex7<-plot(mpg,cyl);jpeg(filename = "Rplot.jpg",
+ width = 480, height = 480, units = "px", pointsize = 12,
+ quality = 75,
+ bg = "white", res = NA, family = "", restoreConsole = TRUE,
+ type = c("windows", "cairo"), ex7)

b.ex7<-plot(mpg,cyl);save.plot(filename = "Rplot.jpg",
+ width = 480, height = 480, units = "px", pointsize = 12,
+ quality = 75,
+ bg = "white", res = NA, family = "", restoreConsole = TRUE,
+ type = c("windows", "cairo"), ex7)

Exercise 8

Finally we want to produce a summary of what we have learned from these exercises. So, prepare one plot with 4 variables (see previous exercises), personalize them and save them into a jpeg file.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)