R plotting why not starting from (0,0)? - r

I'm new to R programming and graphics. I'm trying to draw a graph but the (0,0) point is not at the right place. Can someone please tell me how to fix it? Thanks.
Here's my code:
layout(matrix(c(0,0,0,0,0,
0,1,3,2,0,
0,0,0,0,0), nc = 5, byrow = TRUE),
widths = c(lcm(2), 1, lcm(2), 1, lcm(2)),
heights = c(lcm(2), 1, lcm(2)))
layout.show(3)
box("outer", lty = "dotted")
plot.new()
plot.window(xlim=c(-200,0),ylim=c(0,21))
box()
axis(1)
axis(4)
rect(-113,0,0,1,col='cyan')

you can go to see 'xaxs' in ?par

Related

Legend in barplot R

I am trying to make a bar graph in R, but I have a permanent recurring problem. The legend appears wrong and I can't fix it.
If anyone knows how to fix it, I will be very grateful. I attach a screenshot from R:
Thanks!
I'm not sure what the exact issue is here since you posted a picture instead of a reproducible example, but the following code will give you the plot you want:
dev.off()
par(mai = c(1, 1, 1, 2))
barplot(table(cuestionario$genero),
main = "genero",
col = c("blue", "pink"),
ylab = "Cantidad",
ylim = c(0, 400),
beside = TRUE,
legend.text = TRUE,
args.legend = list(x = 3, y = 300))

Align grid() to plot ticks

When adding ticks to a plot (more ticks than default), how does one get the grid() to align the grid to the ticks?
plot(1:10,las=1,xaxp = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
grid(lwd=2, nx=10, ny=10)
Tried changed the xlim and different numbers for the nx arg in grid (number of cells), but the grid simply doesn't line up.
Related, but doesn't answer question: Aligning grid lines in R, bReeze package
Related, and uses workaround: Align grid with ticks
Is the workaround the most efficient option?
You could use abline to draw grids. You can specify where the grids should be with h (for horizontal lines) and v (for vertical lines)
#Plot
plot(1:10,las=1,xaxp = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
#Add horizontal grid
abline(h = c(0,2,4,6,8,10), lty = 2, col = "grey")
#Add vertical grid
abline(v = 1:10, lty = 2, col = "grey")
Another workaround is to use axis where tck value is 1. With axis, you can specify where the grids should be with at
#Plot
plot(1:10,las=1,xaxp = c(0, 10, 10),xlim=c(0,10), ylim=c(0,10))
#Add horizontal grid
axis(2, at = c(0,2,4,6,8,10), tck = 1, lty = 2, col = "grey", labels = NA)
#Add vertical grid
axis(1, at = 1:10, tck = 1, lty = 2, col = "grey", labels = NA)
#Add box around plot
box()
The problem is that grid is putting nx grid lines in the user space, but plot is adding 4% extra space on each side. You can take control of this. Adding xaxs="i", yaxs="i" to your plot will turn off the extra space. But then your upper right point will be cut off, so you need to change the xlim and ylim values and change nx to match. Final code is:
plot(1:10,las=1,xaxp = c(0, 10, 10),xlim=c(0,11), ylim=c(0,11),
xaxs="i", yaxs="i")
grid(lwd=2, nx=11, ny=11)
The answer to your question
When adding ticks to a plot (more ticks than default), how does one get the grid() to align the grid to the ticks?
is:
Using function axis to obtain the x axis tick locations created by plot function in combination with abline
Concretely, you substitute the line
grid(lwd=2, nx=10, ny=10)
by the following three lines
x_ticks <- axis(1, 0:10, labels = FALSE)
grid(lwd = 2, ny = NULL, nx = NA)
abline(v = x_ticks, lwd = 2, lty = 3, col = "lightgray")
and the result will be
You can control both x ticks and y ticks and get rid of the grid function. In this case the 3 lines would be
x_ticks <- axis(1, 0:10, labels = FALSE)
y_ticks <- axis(2, labels = FALSE)
abline(v = x_ticks, h = y_ticks, lwd = 2, lty = 3, col = "lightgray")
I would vote for the workaround. Because if you look at manual from ?grid, it has this statement,
"Note: If more fine tuning is required, use ‘abline(h = ., v = .)’
directly."

R: Positioning graph elements using cm units with generic X-Y Plotting functions

I am sure this is not new for the R community, but is new to me and can't find a clear answer.
Assuming this example:
plot(1:10, xlab="", xaxt="n") # supress OX axis
title(xlab="How can I use cm?", line=2.5)
axis(side=1, at=1:10, line=0.2)
Here I used line argument in function title() to place a label at 2,5 lines of text "outwards from the plot edge" (as described in ?title help). Is there any argument that can take cm, or a way to use cm? Also, how can I find out how many cm does a line of text contains (if there is no other way around)?
Would also be great to know/set the margins in cm and not only like par("mar") [lines of text] or par("mai") [inches]. Is there a way to do that?
Using the line2user function from this answer you can convert centimeters to a "line" then convert the line to user coordinates and add things to the plot using xpd = TRUE:
cm2line <- function(x) {
lh <- par('cin')[2] * par('cex') * par('lheight')
inch <- x/2.54
inch/lh
}
par(mai = rep(5/2.54, 4))
plot.new()
box()
mtext("hello", side = 3, line = cm2line(2))
abline(h = line2user(cm2line(1:5), side = 4), xpd = TRUE)
abline(h = line2user(cm2line(1:5), side = 1), xpd = TRUE)
abline(v = line2user(cm2line(1:5), side = 2), xpd = TRUE)
abline(v = line2user(cm2line(1:5), side = 3), xpd = TRUE)

barplot labels in R: add horizontal lines below the plot region

My code:
x <- c(10, 50, 20, 40)
barplot(x, names.arg=LETTERS[1:4])
What I want is:
I made this figure with the help of R and Adobe Acrobat. I am wondering can I obtain this figure using pure R code?
You can add text with mtext
mtext("E", side = 1, line = 3, adj = 0.375)
mtext("F", side = 1, line = 3, adj = 0.875)
and then draw line with lines but indicating xpd=T
lines(c(0,3.5),c(-10,-10),xpd=TRUE)
lines(c(3.8,4.8),c(-10,-10),xpd=TRUE)
However, you need manually adjust it.
Thanks to Pascal. I got another answer.
x <- c(10, 50, 20, 40)
barplot(x, names.arg=LETTERS[1:4])
mtext("E", side = 1, line = 3, adj = 0.375)
mtext("F", side = 1, line = 3, adj = 0.875)
axis(1, at=c(0.5,1,2,3,3.3), line=2.5, tick=T, labels=rep("",5), lwd=2, lwd.ticks=0)
axis(1, at=4+c(0.1,0.2,0.3,0.4,0.5),line=2.5,tick=T,labels=rep("",5), lwd=2, lwd.ticks=0)

Extending axes to encompass all data in R

I feel like this is a very basic question but I have spent a lot of time looking for an answer and haven't found one. So, if this is answered somewhere else I would love to be redirected rather than downvoted, please.
Anyway, my problem is that when I graph in R, often the y-axis will fail to extend to the end of my data. A sample graphic is below, where you can see that it would be better for the axis to go all the way to 30 rather than 20. However, submitting ylim = c(0,30) doesn't do anything and I cannot think of or find another command that would do the trick?
Here is a reproducible example. If ylim usually works then I am assuming something is breaking because of the aesthetic changes I've made?
set.seed(1)
x<-runif(1:1000, min=1, max=10)
hist(x, breaks=100, main=NA, axes=F, xlab = NA, ylab = NA)
axis(side = 1, tck= -.01, labels=NA)
axis(side = 2, tck=-.01, labels=NA)
axis(side = 1, lwd=0, line= -.4, cex.axis=1.4)
axis(side = 2, lwd=0, line=-.4, las=1, cex.axis=1.4)
mtext(side = 1, "Percent pathogenic bacteria", line = 2.5, cex=1.8)
mtext(side = 2, "Frequency", line = 2.5, cex=1.8)
Use ylim to specify the y axis range:
set.seed(3)
f <- function(y, ...)
hist(y, breaks=20, ...)
ylim <- range(pretty(ceiling(f(y <- rchisq(1000, 3), plot=FALSE)$counts/10)*10))
f(y, ylim=ylim) # versus f(y)

Resources