Omitting y-axis ticks (and labels) in base R - r

The Question:
Imagine I am making a simple plot: plot(1:5,1:5,ylab = "y-axis"). Here, the y-axis (well actually both axis) are not only nicely labeled, but they both have tick marks with labels. How would I go about making a plot with neither tick marks nor tick mark labels?
What I've tried
The first thing I've tried was removing the margins to the left of the graph, while this effectively worked, it was a bit hacky and didn't get at what my actual problem was. I then continued to dive into the par specifications. Setting par(tck = 0) removes y-axis tick marks, but does so by removing all tick marks on the graph - this isn't ideal. I also tried par(ann = FALSE), and par(cex.lab = 0.001, cex.axis = 0.001) which also did not work.
I'm pretty sure the answer to my question is burried somewhere in the par specifications but for now, unfortunately, I will have to stick with removing the left margins to 'cut off' these undesirable labels.

Related

`yaxp` is no longer working like it used to

When I teach histograms using R, I use the yaxp parameter to adjust the y-axis. Here is an example of the code:
hist(bookspending$'Dollars on books', yaxp = c(0,9,9))
This produces a histogram where the y-axis has tick marks at 0, at 9, and a tick mark at every integer in between. Here is a picture of it:
I'm using Rstudio version 1.4.1717 and R version 4.1.1. This has worked the past 4 years. This semester the yaxp parameter isn't working for some students. I replicated the problem using the web-based version of R. Using the same code, the yaxp parameter makes no changes to the histogram. Here is the picture:
Why does yaxp no longer work for the online version, but it works for my version? How do you adjust the y-axis without yaxp?
This happens because there was a change in the function graphics:::plot.hist in R version 4.2.0.
Previously, the part of the function that controlled the drawing of the axes was simply:
if (axes) {
axis(1, ...)
axis(2, ...)
}
But it has been changed to
if (axes) {
axis(1, ...)
yt <- axTicks(2)
if (freq && any(ni <- (yt%%1) != 0))
yt <- yt[!ni]
axis(2, at = yt, ...)
}
Which means that the y axis breaks are now decided solely by the output of the function call axTicks(2). Your yaxp argument is still passed to axis, but unfortunately, specifying at nullifies yaxp.
This is sort of mentioned in the CRAN R News, where it says:
hist.default() gains new fuzz argument, and the histogram plot method no longer uses fractional axis ticks when displaying counts ("Frequency").
So the reason for the change was to prevent fractional axis ticks, but a side effect of this was preventing users from specifying y axis breaks. The only work-around for now it would seem, would be to add the axes manually as suggested in the last set of comments.
You may wish to file this as a bug report, since it seems to me that it should be possible to specify axis breaks and disallow fractional breaks without much difficulty.

In R, how do I set the specific location of an axis label?

I manually added in some tick marks, but these tick marks are too close and thus overlap with numbers already there. I can specify the exact location for these number labels? Here is an example picture of my problem. I tried changing the size, but there is still overlap.
You can use the mtext function to manually add text on the margins. The line and at arguments give the position of the text. See ?mtext for more details.
plot(x=1:4, y=1:4)
abline(h=c(2.6,3.4))
mtext(c(2.6, 3.4), side=2, line=0, at=c(2.6, 3.4))
x=1:10
y=10:1
use the xaxt= 'n' to rid yourself of the wrong axis
plot(x,y,xaxt='n')
then use the axis function to get them exactly where you want
l1=1:10
at1=1:10
axis(1, labels=l1,at=1:10)
now add a new tick mark somewhere, take out a tick mark if you want.
l2=c(l1[-3],3.14)
at2=c(at1[-3],pi)
plot(x,y,xaxt='n')
axis(1, labels=l2,at=at2)

Lattice in R: Bottom of bar charts not aligned with x=0

I tried making a simple bar chart using the barchart function from the Lattice library with the following code
plot_sum_h <- barchart(event~value|incidence, data=msum_h_combo,
groups=variable,
ylab="Event", xlab="", layout=c(2,1),
main="Total Number of Casualities",
scales=list(y=list(relation="same"),
x=list(relation="free"))
)
and got
this graph. Links not working anymore.
The bottoms of the bars, which should be aligned with the zero mark on the x-axis are behind x=0. Does anyone have any idea what might have gone wrong? Any advice would be appreciated.
You can set that by setting limits on the x-axis. In your scales argument you can do
x=list(relation="free", limits= list(c(0,2000),c(0.20000)) )
In the limits list this will set the scales for each panel (when you use relation="free")
That will put the edge of the graph at 0, and I just guessed what would be good for the upper limits. You could also set limits like =c( 0,( 1.1*max(values) )) Which would set the max at 110% of the maximum of its Value in each panel.

ggplot2 polar plot axis label location

This is just a extension for a old question
ggplot2 polar plot arrows
You will find the x axis is out of the most_out circle.
In ggplot2, I use "panel.grid.major = theme_line(colour = "black", size = 0.2, linetype=2)" to get the dashed circle, just as below:
So my question is how to make the axis label (180, 135, 90, .....) outside of the circle, because the text are merge with the circular lines.
I try to use "hjust" or "vjust" to adjust the distance between text and axis. But it does not work.
So do you have some ideas about this problem?
Thanks first!!!!
You have not provided code to reproduce the problem so this will be just a guess.
I've used whitespace, \n in particular, to move text "away" in the past. Perhaps a custom formatter might work here. Here is how you can write a custom tick mark label formatter.
If this fails, you can always hide the axis labels and paint them yourself using geom_text by adding another layer.
Hope this helps. #hadley's book on ggplot2 is very good, by the way.
I came across this question while I was trying to fix a similar issue myself. One workaround is pretty much covered in the answer to this post: Remove extra space and ring at the edge of a polar plot
You would have to adjust the limits of the x scale to match your axis labels. You would also have to create a new scale bar corresponding to the radial length of your arrows (the 0-300 scale bar on the left side of your plot), since
axis.text = element_blank
takes the scale bar away as well.

R/Zoo: show a tick every year on x-axis

I've a zoo object, with a yearqtr index, covering about 50 years. When plotted the x-axis shows labels every 10 years, which feels a bit barren:
b=zoo(1:200,as.yearqtr(1900+seq(1,200)/4))
plot(b)
Some study got me this:
plot(b,xaxt="n");axis(1,time(b))
Which feels like swinging from one extreme to the other, as the x-axis is a blur of ticks, with ugly fractional labels. Is there an easy way to have it just show years? (What I was looking for initially was a way to say: "lower the x-axis label spacing a bit", but there seems nothing like that? cex.axis just alters the font-size.)
Did you read help(axis)?
Here is one way, just creating a simple index every four quarters:
R> ind <- seq(1, length(b), by=4)
and using it to index the axis placement and labels:
R> plot(b,xaxt="n")
R> axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.5)
I used las=2 and the lower cex value to make this fit. Once every year may still be too plenty.
Computing "good" axis labels is really hard.
This is probably one of those (rare) situations when you want to use grid rather then ticks to better show your data. As #dirk-eddelbuettel pointed out - tweaking good axis labels is hard, especially with such density. You also might want your labels inside plot, so the grid will slightly hide their density. The easiest grid to get is with abline, unless you want to play with ggplot2, but it's uglier then standard plots in R (personal opinion). Also - make the plot wider. In fact, it's better to get rid of box around plot too ;) Below is mod of Dirk's approach:
png("strangeplot.png",width=800)
#extend y-axis to fit inside labels and remove box
plot(b,type="n",xaxt="n",yaxt="n",ylab="",xlab="",ylim=c(min(b)-30,max(b)),bty="n"))
#use 'mpg' to get labels inside
axis(1,time(b)[ind], format(time(b)[ind]), las=2, cex.axis=0.6,tick=F,mgp=c(0,-2.5,0))
axis(2,tick=F,las=1)
#you locate lines slightly to the left of label...
abline(h=seq(0,200,by=50),v=time(b)[ind]-0.5,col=gray(0.9))
#...so you need to add extra single line in the end
abline(v=max(time(b)[ind])+0.5,col=gray(0.9))
#plot at the end to get it above grid
points(b,type="l")
dev.off()

Resources