How to add squares to the back of my R plot? [duplicate] - r

Is there a command to easily add a grid onto an R plot?

The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,
abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")
Good luck!

See help(grid) which works with standard graphics -- short example:
R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()
The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.
By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.

If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:
plot(cumsum(rnorm(100)), type='l', panel.first=grid())
The plot.default() documentation provides more information about these parameters.

I agree with cbare.
Use abline to draw lines only where you really need.
Example from my last code:
abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted")
remember that:
v is for vertical lines.
h for horizontal.
exploit the commands
lty for dotted line
color for light coloured line
in order to obtain "no heavy grid".

Another option is using the axis function for vertical and horizontal grid lines:
x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Only horizontal grid
plot(x)
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
Created on 2022-08-20 with reprex v2.0.2
You can specify the position of the grid lines using the at argument.

Related

Can't get axis labels to show on r plot()

I'm working with the meuse dataset in the sp library in R and I'm just trying to obtain a simple plot of the meuse grid which highlights the different areas of flooding frequency. However, I can't seem to get the axis labels to display. I've tried using a par() statement beforehand but it doesn't appear to be doing anything?
data(meuse.grid) #in sp library
summary(meuse.grid)
str(meuse.grid)
coordinates(meuse.grid) = ~x+y
proj4string(meuse.grid)<-CRS("+init=epsg:28992")
gridded(meuse.grid)=TRUE
class(meuse.grid)
par(mar=c(10,10,4,2)+0.1,mgp=c(5,1,0))
plot(meuse.grid["ffreq"], scale.frac = 0.6,main="Flooding Frequency Class Map",
xlab="Easting",ylab="Northing",axes=TRUE)
Any suggestions?
You could use mtext as a fix, expand slightly outer margins oma in advance. You could also fix the title with this method.
par(mar=c(10,10,4,2) + 0.1, mgp=c(5,1,0), oma=c(2, 2, 2, 2))
plot(meuse.grid["ffreq"], scale.frac = 0.6,main="",
xlab="",ylab="",axes=TRUE)
mtext("Easting", side=1, line=3, font=2)
mtext("Northing", side=2, line=3, font=2)
mtext("Flooding Frequency Class Map", side=3, line=1, font=2, cex=1.2)
Try reducing the plot margins by setting par() before your plot() function. The default values are:
par(mar = c(5, 4, 4, 2) + 0.1)
where each number represents a side of the plot (bottom, left, top, right). setting the outer margins via par(oma) (in a similar way to above) might also help.

How to I resize the outline of a pch plot point in base R?

I'm tinkering with cex options in base R plots to resize my plot for a presentation slide. I seem to be able to tweak most size aspects, but I notice the outline color of my pch point is not getting any bolder/thicker when the plot point becomes larger. So the larger the plot point, the less noticeable the outline color.
Found many websites (& SO posts) on various cex options (cex, cex.main, cex.sub, cex.axis, cex.lab), but none seem to be adjusting the pch plot point.
I'm aware that only certain pch symbols (21 to 25) can be used with fill color and outline. My sample code uses 21 (circle).
data("mtcars") # test data
summary(mtcars[c("hp","mpg")]) # to find on min & max values to set plot limits
# set general features used for multiple plots
par(bg="blue", fg="red", col="yellow", col.axis="white", col.lab="white", bty="n", cex=1.5)
# test plot to illustrate, cex used here to further adjust plot points from par setting
plot(mtcars$hp, mtcars$mpg, ylim=c(10, 35), xlim=c(50, 340), pch=21, bg="red", cex=2)
Am I missing something about cex or is there a different solution without turning to ggplot? I'm not adverse to ggplot if there is an option, but I'd like to see if it can be done in base R.
You need to specify lwd to change the border thickness of pch symbol
plot(1:10, 1:10, pch = 21, cex = 3, lwd = 1:10)

Is there an easy way to place grid in plot to background R [duplicate]

Is there a command to easily add a grid onto an R plot?
The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,
abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")
Good luck!
See help(grid) which works with standard graphics -- short example:
R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()
The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.
By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.
If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:
plot(cumsum(rnorm(100)), type='l', panel.first=grid())
The plot.default() documentation provides more information about these parameters.
I agree with cbare.
Use abline to draw lines only where you really need.
Example from my last code:
abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted")
remember that:
v is for vertical lines.
h for horizontal.
exploit the commands
lty for dotted line
color for light coloured line
in order to obtain "no heavy grid".
Another option is using the axis function for vertical and horizontal grid lines:
x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Only horizontal grid
plot(x)
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
Created on 2022-08-20 with reprex v2.0.2
You can specify the position of the grid lines using the at argument.

how to create a plot with customized points in R?

I know I can create a plot with line and dots using the type = "o" argument in the plot command. I would like some more control over this -- I want to be able to draw the "o" as full dots, with black border and fill-in color of my choice, of customized size and of a different color than the line. Same for the line, I want to make it thicker, and of my choice of color. How would I go on about doing that?
What I found until now is just a plain
plot(y, type= "o")
which is too poor for my needs.
I am not interested in using ggplot, but instead use the internal plot library of R.
Any help appreciated.
All the information you need should be present in ?plot and ?points, as suggested by #BenBolker. In particular, you want to be using pch=21, and specifying background colour with the bg argument, size with cex, and line width with lwd.
If you want the line to be a different thickness to the point borders, you need to plot the line first, and then overlay the points.
For example:
y <- sample(10)
plot(y, lwd=6, type='l')
points(y, bg='tomato2', pch=21, cex=3, lwd=3) # tomato2 is a personal fave
You could also provide a vector of lwd, cex and col to the points call:
plot(y, lwd=6, type='l')
points(y, bg=rainbow(10), pch=21, cex=seq(1, by=0.2, length.out=10),
lwd=seq(2, by=1, length.out=10))
You could use layering (I don't work in base too much any more as a social researcher I love the facet_grid of ggplot, so there may be a better way) as in:
x <- sort(rnorm(25))
y <- sort(rnorm(25))
z <- as.factor(sample(LETTERS[1:5], 25, r=TRUE))
plot(x, y, pch = 19, cex = 1.3)
par(new = TRUE)
plot(x, y, pch = 19, cex = 1, col = z)
Which gives you:

Grid in an R plot

Is there a command to easily add a grid onto an R plot?
The grid command seems to draw grid lines where-ever it feels like. I usually use abline to put lines exactly where I want them. For example,
abline(v=(seq(0,100,25)), col="lightgray", lty="dotted")
abline(h=(seq(0,100,25)), col="lightgray", lty="dotted")
Good luck!
See help(grid) which works with standard graphics -- short example:
R> set.seed(42)
R> plot(cumsum(rnorm(100)), type='l')
R> grid()
The ggplot2 package defaults to showing grids due to its 'Grammar of Graphics' philosophy. And lattice has a function panel.grid() you can use in custom panel functions.
By the way, there are search functions for help as e.g. help.search("something") and there is an entire package called sos to make R web searches more fruitful.
If you are not using a custom tick interval, you can control the grid and axes parameters directly from the plot() command:
plot(cumsum(rnorm(100)), type='l', panel.first=grid())
The plot.default() documentation provides more information about these parameters.
I agree with cbare.
Use abline to draw lines only where you really need.
Example from my last code:
abline(v=c(39448, 39814), col="grey40")
abline(h=c(-0.6, -0.4, -0.2, 0.2,0.4,0.6), col="grey10", lty="dotted")
remember that:
v is for vertical lines.
h for horizontal.
exploit the commands
lty for dotted line
color for light coloured line
in order to obtain "no heavy grid".
Another option is using the axis function for vertical and horizontal grid lines:
x <- rnorm(100)
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
# Only vertical grid
plot(x)
# Vertical grid
axis(1, tck = 1, lty = 2, col = "gray")
# Only horizontal grid
plot(x)
# Horizontal grid
axis(2, tck = 1, lty = 2, col = "gray")
Created on 2022-08-20 with reprex v2.0.2
You can specify the position of the grid lines using the at argument.

Resources