Plotting 2

[This article was first published on The Practical R, 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.

In this post we’ll cover go into more detail on plotting commands. We’ll use a scatterplot (X-Y plot) as our example plot. Again we’ll use the command plot.

##First let's make some data
x<-c(1,3,5,7,9,11)
y<-c(2,4,6,8,10,12)

plot(x,y)

Next let’s change the axis labels. To change the axis titles we’ll use the commands xlab and ylab for the x-axis and y-axis, respectively. We add these calls within the parenthesis of the plot function. Let’s make the x-axis “Even” and the y-axis “Odd”.

plot(x, y, xlab = "Even", ylab = "Odd")

Looks good! Now let’s change the x- and y-axis limits. We’ll use the commands xlim and ylim. In each case we give a lower and upper limit, so we need to concatenate them together with the c function. In our example we’ll set the x-axis from 0 to 15 using xlim = c(0, 15), and the y-axis from 1 to 20 using ylim = c(1, 20). Again these commands are added within the plot function.

plot(x, y, xlim = c(0, 15), ylim = c(1, 20), xlab = "Even", ylab = "Odd")

Next let’s add a title calling it “My Plot”. We’ll use the command main = “add your title here”.

plot(x, y, main = "My Plot", xlim = c(0, 15), ylim = c(1, 20), 
     xlab = "Even", ylab = "Odd")

Now, let’s spice up the colors of our plot. Let’s make the points red and bigger. We use the calls “col” and “cex” to adjust these items.

plot(x, y, col = "red", cex = 2, main = "My Plot", 
     xlim = c(0, 15), ylim = c(1, 20), xlab = "Even", 
     ylab = "Odd")

Now let’s make our points a little bit fancier. We can use the command pch to change the points from the standard hollow circle to a filled diamond (pch = 18). You can find a snapshot of the different pch symbols here. Since this is a filled symbol, the call col colors the outline and the call bg colors the fill of the symbol.

plot(x, y, pch = 23, bg = "yellow", col = "red", 
     cex = 2, main = "My Plot", xlim = c(0, 15), 
     ylim = c(1, 20), xlab = "Even", ylab = "Odd")


Finally let’s complete the plot by adding a legend. The legend is different than the previous calls. It goes outside of the plot() command. Add the legend() command on a second line. The first bit of code “topleft” adds the legend to the top left of the plot. The second bit of code calls the legend item by the name “my data”. The rest of the code defines the legend item as we added it into the plot. The exception is the call “pt.bg” which has to be used instead of just “bg”.

plot(x, y, pch = 23, bg = "yellow", col = "red", 
     cex = 2, main = "My Plot", xlim = c(0, 15), 
     ylim = c(1, 20), xlab = "Even", ylab = "Odd")
legend("topleft", "my data", pch=22, pt.bg="yellow", col="red")

That’s it for now. We’ll do some more plotting next time!


To leave a comment for the author, please follow the link and comment on their blog: The Practical R.

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.

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)