Tutorial: Using plot() function
[This article was first published on ALSTAT R Blog, 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.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Hello Readers! This is my first post as a member ofR-bloggers. In this post I’m going to talk about the basic plotting in R, fortwo dimensional. This is a tutorial for beginners in R.
Then, here’s the simplest way on plotting a yfunction using points on vector x.
Now, you can modify your plot. Say you want to add atitle on your plot. Then you can just add main=”Plot of sin(x) + cos(x)”, on the code. That is,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(x, y, main = "Plot of sin(x) + cos(x)") |
Adding label on both axes is achieved using xlab andylab options. Thus, we have,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(x, y, main = "Plot of sin(x) + cos(x)", xlab = "x values", ylab = "sin(x) + cos(x)") |
“p” for points,
“l” for lines,
“b” for both,
“c” for the lines part alone of”b”,
“o” for both overplotted,
“h” for histogram like (or high-density)vertical lines,
“s” for stair steps,
“S” for other steps,
“n” for no plotting.
So let’s say we choose type “h”. Then we have
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(x, y, main = "Plot of sin(x) + cos(x)", xlab = "x values", ylab = "sin(x) + cos(x)", type = "h") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(x, y, main = "Plot of sin(x) + cos(x)", xlab = "x values", ylab = "sin(x) + cos(x)", type = "h", lwd = 2.5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plot(x, y, main = "Plot of sin(x) + cos(x)", xlab = "x values", ylab = "sin(x) + cos(x)", type = "h", lwd = 2.5, col = “red”) |
In addition, you can also change the plotting’character’, by using the pch option in R.
To leave a comment for the author, please follow the link and comment on their blog: ALSTAT R Blog.
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.