How to add grids to plots in R


  R Interview Questions

The plot function in Base R does not automatically put up a grid. For example, if you do a simple plot like this,

# Plot y = square(x) for x between 1 and 10
> plot(1:10,(1:10)^2)

It is pretty hard to figure out where exactly each of these points lie. A grid would be nice to quickly get the x and y values.

# nx stands for the number of vertical lines on the x-axis.
# ny = nx is a default argument
> grid( nx= 10 )

The default grid lines are grey in color. You can change it by specifying the col argument.

> grid( nx = 10, col = "lightblue")

Check out this post on how to make interactive plots in R using plotly

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.