R language: plot grid lines only inside the graph - plot

I am trying to remove all grid lines outside the graph. I noticed that behavior in R is not deterministic, i.e., sometimes grid lines are inside the graph only (as I want), but sometimes it spans an entire figure (see sample). I'd like to always put grid lines inside.
I read grid manual, but could not find an option to do so. abline() also puts grid lines across an entire figure.
The code I am using is
plot(xrange, yrange, type="n", xlab="X", ylab="Y", xlim=c(200,1500), ylim=c(0,10000))
...
grid(lty=3, col="gray")
Any help is appreciated. Thanks,
Nodir

When I have had this problem it is because par(xpd=TRUE) is somewhere in the code. Try setting par(xpd=FALSE) before using grid() and then par(xpd=TRUE). The sample code was used to generate the same the two plots, one of which has the grid lines extending outside of the plot region.
set.seed(1)
x <- rnorm(100)
y <- rnorm(100)
# scatter plot with gridlines inside
par(xpd=FALSE) # do not plot outside the plot region
plot(x,y)
grid(lwd=2)
# scatterplot with gridlines outside the region
par(xpd=TRUE) # plot outside the plot region
plot(x,y)
grid(lwd=2)

Related

R plot and barplot how to fix ylim not alike?

I try to use base R to plot a time series as a bar plot and as ordinary line plot. I try to write a flexible function to draw such a plot and would like to draw the plots without axes and then add universal axis manually.
Now, I hampered by strange problem: same ylim values result into different axes. Consider the following example:
data(presidents)
# shorten this series a bit
pw <- window(presidents,start=c(1965))
barplot(t(pw),ylim = c(0,80))
par(new=T)
plot(pw,ylim = c(0,80),col="blue",lwd=3)
I intentionally plot y-axes coming from both plots here to show it's not the same. I know I can achieve the intended result by plotting a bar plot first and then add lines using x and y args of lines.
But the I am looking for flexible solution that let's you add lines to barplots like you add lines to points or other line plots. So is there a way to make sure y-axes are the same?
EDIT: also adding the usr parameter to par doesn't help me here.
par(new=T,usr = par("usr"))
Add yaxs="i" to your lineplot. Like this:
plot(pw,ylim = c(0,80),col="blue",lwd=3, yaxs="i")
R start barplots at y=0, while line plots won't. This is to make sure that you see a line if it happens that your data is y=0, otherwise it aligns with the x axis line.

R - histogram rectangles line thickness

How can I get the actual lines (that form the many rectangles) on a histogram to be thicker? I would like to avoid using ggplot.
Here is some code that generates a histogram so that we have a reproducible example:
h = hist(rnorm(100),plot=F)
plot(h,lwd=4) #note, lwd does not work :(
You can set the line width with par():
opar <- par(lwd=2)
plot(h)
par(opar)

setting mfg in a multi plot prevents drawin on margin

I'm trying to add common axes to a bunch of plots by putting them in the outer margin.
Plots are drawn first in a loop (not in the example) then I wanted to draw axes on the bottom of the two rows of plots.
But drawing the axis outside the plotting region is only possible without mfg being changed. How can I enable out-of-plot-drawing after changing mfg?
par(mfrow=c(2,2),
mar=c(1,1,0,0),
oma=c(3,0,0,0))
#Some plots
plot(function(x)x^2,from=-1,to=2, frame.plot=T,axes=F)
plot(function(x)x^3,from=-2,to=2, frame.plot=T,axes=F)
plot(rnorm(10), frame.plot=T,axes=F)
plot(1:10, frame.plot=T,axes=F)
# axis on last drawn plot (mfg=c(2,2)) - works
axis(side=1,line=0,outer=TRUE)
# set mfg to same value (mfg=c(2,2))
par(mfg=c(2,2))
# red axis is clipped to plot region, even with xpd?
axis(side=1,line=-.2,outer=FALSE,xpd=NA,col="red")
par(mfg=c(2,1))
axis(side=1,line=-.2,outer=FALSE,xpd=NA,col="red")
You can set :
par(xpd=NA)
to make sure that the axis is not clipped to the plotting region.

Making square axes in R

How can I use R to make axes always square in scatter plots? for example in:
> plot(iris)
or
> plot(iris$Petal.Width, iris$Petal.Length)
I'd like the axes to be square, i.e. the same length and tick labels for the x and y axes.
The current proposed answer does not work: the call,
plot(iris$Petal.Width, iris$Petal.Length, xlim=c(0,10), ylim=c(0,10), asp=1)
Generates:
which is not square, and does not have the same axis ticks and tick labels. The spaces between the x tick labels must be the same and the plot should be square, not rectangular.
You need to also set pty="s" in the graphics parameters to make the plot region square (independent of device size and limits):
par(pty="s")
plot(iris$Petal.Width, iris$Petal.Length, asp=1)
lines(2+c(0,1,1,0,0),3+c(0,0,1,1,0)) # confirm square visually
First of all, for me the plot already comes out square (big image). Clearly for you this is not the case, and you might need to make plots larger than the screen anyhow.
So, the size of the plot is controlled by the size of the output area, ie the plot window, the image file, or whatever else. Using Rstudio, you can use the built-in GUI the specify plot size. If you insist on using the base R console, you'll need to manually do the exporting. First open the file:
png("image.png", width=600, height=600)
This will open an image file in the working directory with equal proportions. Now plot:
x = iris$Petal.Width
y = iris$Petal.Length
all = c(x,y)
range = c(min(all), max(all))
plot(x, y, xlim=range, ylim=range)
And close the file:
dev.off()
The result:

R: square plotting region lost when plotting as pdf

This has certainly been asked before, but I can't find a solution. I would like to plot some data with a perfect square as bounding box. With par(pty="s") this is easy, but once I save the plot as a pdf file, the square is lost. As I understand, this is because pdf() uses paper="special" as default and thus respects width and height (which does not give a perfect square since we have axis labels). But specifying other paper options did not help.
## the bounding rectangle in the following plot is a perfect square...
U <- matrix(runif(1000), ncol=2)
par(pty="s")
plot(U, type="p", xlab="U_1", ylab="U_2")
## ... however, not anymore if the plot is generated as a .pdf
pdf(file="U.pdf", width=6, height=6)
par(pty="s")
plot(U, type="p", xlab="U_1", ylab="U_2")
dev.off()

Resources