Customize a scatterplot exercises

March 17, 2016

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

plot exercises-9In the following exercises we practice how to customize a scatterplot. We will use axis , to add an axis; mtext to add a text; and legend to add a legend. Moreover we practice how to add details in every stage. We will use the mtcars dataset, provided by R Cran (we can upload dataset by type mtcars and then attach our dataset by attach(mtcars)). A description of the data is available here.

This set is the second set of exercises is a series on data visualization. Click here for the other sets in the series. For the 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
Create a scatterplot of mpg (y-axis) against drat (x-axis) and add a label to the x-axis. Which of the following statements is correct:
a. plot(mpg,drat,xaxis="Miles per gallon")
b. plot(mpg,drat,xlab="Miles per gallon")

Exercise 2
We just saw how to customize the label on the x-axis. For the next exercise:
a. Customize the y-axis label like we customized the x-axis in the previous exercise..
b. Produce a plot customizing x and y axis, range and colours. Choose a range for x axis from 0 to 40, for y axis from 0 to 7, red colour and highlighted points.

Exercise 3
We have to add a title to our plot. What command do we have to type?
a. plot(mpg,drat,main="Miles vs Rar")
b. plot(mpg,drat,title="Miles vs Rar")

Exercise 4
We just saw how to add labels, titles and custom details such as colours and the size of points. Now we will
have to construct our plot in different stages. Firstly we have to plot our data, secondly we will add axes, title and text.
a.Plot our data specifying that axes haven’t to be plotted.
b.Add axes, labels and text afterwards

Exercise 5
Now we want to add a legend to our plot. Which statement is correct?
a. plot(mpg,drat,legend=1)
b. plot(mpg,drat);legend()

Exercise 6
Customize our legend:
a.Use different types of symbols, colours, background colours and position.
b.Insert new variables to our plot and then customize the legend

Exercise 7
Finally, we will build a plot using four continuous variables in two stages:
a. plot two variables at a time, eliminating axes. Introduce axes and labels afterwards.
b. Insert a legend using diffent colours and adifferent symbol for every variable. Put the legend in a top right position using x and y coordinates.

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.


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)