Graphics parameters exercises

March 24, 2016
By

(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?

a.par(col=2,lty=2,bg=4);plot(mpg,wt)
b.plot(mpg,wt,col=2,bg=4)

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?

a.par(mfrow=c(1,2));plot(mpg,wt);plot(mpg,hp)
b.par(mfrow=c(2,1));plot(mpg,wt);plot(mpg,hp)

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?

a.dev.off=0
b.dev.off()

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?

a.windows();plot(mpg,cyl)
b.dev.in();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.

R-bloggers.com 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.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training



http://www.eoda.de









ODSC

CRC R books series











Contact us if you wish to help support R-bloggers, and place your banner here.

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)