In the exercises below we practice the basics of visualization in R. Firstly, we use the command:
plot . Then we will see how to add information to our plot through command:
lines . 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.
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.
Plot Miles/(US) gallon versus Rear axle ratio by
plot(mpg,drat) . On which axis does mpg appear?
a. mpg appears on the x axis
b. mpg appears on the y axis
Is a scatterplot recommended for continuous or dichotomous variables?
Produce a histogram with
hist(gear). What do you see?
b. probability density
Change type of visualization of our scatterplot in Exercise 1
plot(mpg,drat,type=""). If we want to see lines what we have to type into “”:
Now we want to see both point and lines in our plot. What we have to type into
Add another variable to our plot, for example Weight. What command do we have to use:
plot(mpg, drat); plot(mpg, wt)
plot(mpg, drat); points(mpg, wt)
Now we have added a new variable to our plot. Suppose we want to use two different colours to separate the points. Type
plot(mpg, drat, col=2) :
What colour have we selected:
Now we want to differentiate the two different variables in the scatterplot:
a. Let’s change the colours of the second plot
b. Change use two different types of plot (e.g. points,lines)
Now we want to highlight a variable in the final plot.
plot(mpg, drat, lwd=2) ; points(mpg, wt, lwd=1). Which plot is highlighted:
a. plot1 (mpg,drat)
b. plot2 (mpg,wt)
Finally choose four different continuous variables from mtcars set and produce:
a.Plot with lines and points for different variables with different colours (hint: change
y axis parameters by adding command
ylim=c(0,30) to plot [e.g.
b.Choose one variable from each and highlighted it set red colour and a broad line
John Wilson says
I’d pay you a subscription if it meant the site didn’t carry the Ads.