Start plotting data!

March 11, 2016
By

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

plot exercises-6In 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

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.



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

Sponsors

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)