+4 votes
in Programming Languages by (5.4k points)

I am using the plot() function to generate a line plot in R. It does not automatically add gridlines to the plot. How can I add grid lines?

1 Answer

0 votes
by (33.7k points)

The grid() function can be used to add an MxN rectangular grid to an existing plot. The syntax of this function is as follows:

grid(nx = NULL, ny = nx, col = "lightgray", lty = "dotted", lwd = par("lwd"), equilogs = TRUE)

nx, ny    = number of cells of the grid in x and y direction, respectively.

Here is an example:

sf <- c(2828978234.6720667, 2523723428.4042473, 2493970473.7634807, 2478117503.102314, 2453217115.4810367, 2446309458.763538, 2439580319.7837763, 2376784754.3911676, 2362926245.68204, 2354647422.048292, 2343058355.8204017, 2338062923.4045787, 2335978334.3602896, 2327838580.2337847, 2324959353.4350915, 2307427379.9857135, 2305581479.4816375, 2299412512.127759, 2298692239.1688786, 2295844865.3728147, 2294971760.7162547, 2292083426.77864, 2289618975.0881863, 2279046140.6888947)
plot(diff(sf), type = "b", col= "blue", ylab = "BIC Gradient", xlab = "Cluster_count", xlim = c(0,30), pch=16, cex=1.5,)
grid(nx=25, ny=25)

...