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.

**Exercise 1**

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

**Exercise 2**

Is a scatterplot recommended for continuous or dichotomous variables?

a. continuous

b. dichotomous

**Exercise 3**

Produce a histogram with ` hist(gear)`

. What do you see?

a. frequencies

b. probability density

**Exercise 4**

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 “”:

a.type=”l”

b.type=”p”

**Exercise 5**

Now we want to see both point and lines in our plot. What we have to type into

` plot(mpg,drat,type="")`

:

a.type=c(“p”,”l”)

b.type=”b”

**Exercise 6**

Add another variable to our plot, for example Weight. What command do we have to use:

a.`plot(mpg, drat); plot(mpg, wt)`

b.`plot(mpg, drat); points(mpg, wt)`

**Exercise 7**

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:

a. red

b. green

**Exercise 8**

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)

**Exercise 9**

Now we want to highlight a variable in the final plot.

Type: `plot(mpg, drat, lwd=2) ; points(mpg, wt, lwd=1)`

. Which plot is highlighted:

a. plot1 (mpg,drat)

b. plot2 (mpg,wt)

**Exercise 10**

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. ` plot(a,b,type="p",ylim=c(0,30))`

.

b.Choose one variable from each and highlighted it set red colour and a broad line

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

John Wilson says

I’d pay you a subscription if it meant the site didn’t carry the Ads.