R: how to remove this tiny axis margin in plot - r

I want to get rid the small margin close to zero on X and Y value (red line on pic), and plot ONLY what is showed in red square.
I tried setting par(mar = rep(0, 4) and xlim=c(0, ...), ylim=c(0, ...) but R still keeps adding this tiny margin. How to get rid of it?
EDIT:
another point of view on my problem:
after running:
require(plotrix)
axisRange <- c(0,500)
plot(NULL, xlim = axisRange, ylim=axisRange)
draw.circle(0, 0, 200, col = "white", border = "red")
I end up with a circle positioned not in "true" 0,0 point:
EDIT2:
Actually what I want to do, is to plot circles of different radius, and save it as an image. That is why I care about the margins.
I end up with something like this (spots on the corners are for the reference):
And should be more like this:

You can set the xaxs and yaxs arguments to "i" as opposed to the default of "r". From the par help page:
Style "r" (regular) first extends the data range by 4 percent at each
end and then finds an axis with pretty labels that fits within the
extended range.
Style "i" (internal) just finds an axis with pretty labels that fits
within the original data range.
library(plotrix)
axisRange <- c(0,500)
par(mar = rep(0,4))
plot(NULL, xlim = axisRange, ylim=axisRange, xaxs = "i", yaxs = "i")
draw.circle(0, 0, 200, col = "white", border = "red")
Gives:

Related

box without space in multhist

Suppose data
set.seed(42)
a <- rnorm(100)
b <- rnorm(100)+1
which I would like to plot side-by-side using multhist().
multhist(list(a,b), yaxs="i")
Now I would like to draw a box around them
box(which = "plot", lty = "solid")
which gives me
with some space between the bottom line of the box and the bars.
Had I used hist() to plot only one graph, the ouput would have been without gap between box and bars:
Is there a different trick to get such an output in multhist()?
I think setting ylim mentioned by #KamranEsmaeili is a standard solution. Here I provided a tricky way that doesn't require manually setting the upper limit 40.
multhist() is based on the built-in barplot() and it always sets the lower limit of y-coordinate of the plotting region less than 0. You can use par("usr")[3] to check this fact. I just came up with a tricky method that adjusts the box type to "7" to suppress the bottom line and add a new bottom line at 0 by abline(h = 0).
library(plotrix)
set.seed(42)
a <- rnorm(100)
b <- rnorm(100) + 1
multhist(list(a,b))
#---------------------------------
box(bty = "7") # bty is one of "o"(default), "l", "7", "c", "u", and "]".
abline(h = 0)
Edit
If you don't like the right line extending beyond the x axis, then you can replace box() with rect() so that you can specify positions of four sides by yourself. Remember to add xpd = TRUE, or the line width will look thinner than y-axis.
multhist(list(a,b))
x <- par("usr")
rect(x[1], 0, x[2], x[4], xpd = TRUE)
Just add "space=c(0,0)" and "ylim" and you good to go:
multhist(list(a,b), yaxs="i", space=c(0,0), ylim=c(0,40))

Adjust grid in R plot

I'm ploting a vector of 29 values in a plot using:
plot(0:29, v, type="o", main="Title of the plot")
I try to adjust the grid to be unit by unit using:
grid(31, lw=2)
The problem is that x axis first value (0 position) doesn't start at the begining of the graph and grid starts at the begining, so both elements aren't aligned.
How can solve this issue?
Use abline instead.
plot(0:10)
abline(h = 0:10)
abline(v = 0:10)
You can also force the exact x and y limits:
plot(0:10, 0:10, xlim = c(0,10), ylim = c(0, 10), xaxs = "i", yaxs = "i")
grid(10)
In general, using abline is easier and more robust. I also often plot with type = "n", add lines/whatever in the background, then add the points.

Adding label for last point of line in R plot

Consider the following plot:
par(xaxs='i',yaxs='i')
q1 <- c(1000000.0, 908364.8, 876009.1, 847892.8, 824808.3, 805416.2, 785266.2, 770997.1, 753908.6, 744599.9, 706777.6, 674659.9, 634654.4, 601440.4, 568259.7, 535361.3, 493679.9, 465526.5, 429766.6, 395244.7, 361483.2, 332136.6, 308574.5, 285500.6, 262166.2 ,237989.0 , 210766.1, 188578.1, 166762.3 , 140399.8 ,114865.5)
plot(q1, type = "l", lty = 1, lwd = 2, col = "darkolivegreen3", ylim = c(0,4*10^6), xlim = c(1,30), bty = "l")
text(30, q1[30], labels = "text", col = "gray36", cex = 0.8, pos = 4)
I would like to add the label "text" at the right of the last point of the green line (i.e. the point on the line with x = 30).
I tried the code above but the text doesn't show up! Any ideas how to solve that?
Thanks!
By default things in a plot are clipped to the plot region, you are not seeing the text because it has been clipped. You can either use the mtext function to explicitly place the text into the margin. Or if you specify par(xpd=NA) then the clipping will be turned off (well it will still clip to the device region) and the text plotted using the text function will now be plotted extending into the margin. Either way you will probably want to specify some space in the appropriate margin so there is room for the text to be and look nice. See ?par for how to specify the margin and more detail on clipping.
I just realized that this could be done using mtext:
mtext("text", side = 4, at = q1[30], las = 1)

increasing the size of the coloured squares on histogram legends in R

I am trying to increase the size of the coloured squares in the legend on a histogram in R - when I output the PDFs they are too small so it is hard to distinguish the colours. I've searched Google, the R-help Nabble forum and this place, all to no avail. I've also tried several of the commands in the legend documentation.
What do I need to use in the legend() function to increase them? and is it possible to remove the black border around each coloured square to ease viewing?
Here my example:
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
hist(a)
graphics::legend(x=-1,y=10,c(">0%",">20%",">40%",">60%",">80%"),
x.intersp=1,y.intersp=2,cex=1, bty="n",
fill=c("black","gray50","gray70","gray85","white"))
I wish to change the size of box in the legend?
SOLUTION: from #Ben Bolker
add to the script above the legend function
> source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")
then add
> box.cex=c(2,2)
within the legend function
I hacked the source of the legend function to allow a box.cex argument that specifies the relative x and y dimensions of the box. This isn't perfect -- if the expansion is big enough then you have to adjust y.intersp to prevent the fill boxes from overlapping.
source("http://www.math.mcmaster.ca/bolker/R/misc/legendx.R")
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
cex <- 1
hist(a)
legend("topright",c(">0%",">20%",">40%",">60%",">80%"),
bty="n",
fill=c("black","gray50","gray70","gray85","white"),
box.cex=c(3,3),
y.intersp=2.8)
You can use very thick lines, with rectangular ends.
plot( 1, type = "n", axes = FALSE, xlab = "", ylab = "" ) # Empty plot
par( lend = 1 ) # Rectangular line endings
legend(
"topleft",
c( "Red", "Black" ),
col = c("red", "black"),
lty = 1, lwd = 10
)
It is difficult to mix the option fill and change the size of the box.
But we can have something with options pt.cex and pch without fill options.
a<-c(1,1,2,3,3,3,3,4,54,56,2,23,1,3,23)
par(mfrow=c(1,2))
hist(a)
graphics::legend(x=10,y=10,c(">0%",">20%",">40%",">60%",">80%"),
x.intersp=1,y.intersp=1,cex=c(1),bty="n",
fill=c("black","gray50","gray70","gray85","white"),
# pch=c(24,22,21,23,25),
pt.cex = c(2,2,2,2,5),
lwd=1.5,title='Histo fill option')
hist(a)
graphics::legend(x=10,y=10,c(">0%",">20%",">40%",">60%",">80%"),
x.intersp=1,y.intersp=1,cex=c(1),bty="n",
pch=c(24,22,21,23,25),
pt.cex = c(1,2,3,3,4),
lwd=1.5, title = 'Histo whithout fill ')

Scatterplot with axes centered at 0 to look like "crosshairs"

I would like to make a scatterplot for a dataset that contains both positive and negative values. I am very familiar with plot() but I cannot find any option for moving the axes to center of the plot, i.e. at zero. I would like the plot to look like "crosshairs".
I know how to turn off the axes in plot(), i.e. xaxt = "N", and I understand how to use the axis(). There is no option or example that I can find to have the axes centered at zero in the middle of the plot. Using abline() to create the lines and tick marks seems unnecessary.
Can you please point me to a command, trick, or package that I can use with plot() to accomplish this goal?
You need the axis command. Look it up in the help. You can position the x-axis vertically and the y-axis horizontally.
Here's a little example.
plot(c(-2,2), c(-2,2), axes = FALSE, bty = 'n', panel.first = grid())
axis(1, c(-2, -1, 1, 2), pos = 0, cex.axis = 0.8)
axis(2, c(-2, -1, 1, 2), pos = 0, cex.axis = 0.8, las = 2)

Resources