Is it possible to put tick labels of only x-axis inside plotting area?
I am trying:
axis(1,at=c(0:71),c(rep(0:23,3)),cex.axis=.7, font=1,tck=.01)
It seems that:
par(mgp=c(0,-1.4, 0))
puts both x and y tick labels inside plotting area.
Why don't you just draw the ticks where you want them using the pos argument to axis():
plot(0:72, xaxt="n")
text(0:71, -1, rep(0:23, 3), cex = 0.5)
axis(1, at=c(0:71), NA, cex.axis=.7, font=1, tck=.01)
I think the best and easy solution is the parameter tcl in axis or par.
Positive values put the marks inside, negative outside, the value is the length.
Your example:
axis(1,at=c(0:71),c(rep(0:23,3)),cex.axis=.7, font=1,tcl=0.3)
Related
I'm trying to use R to do a barplot. Values I'm plotting range from 0 to 5.0, but are decimal values (such as 4.87) so I don't want to just use the default Y axis, because it just goes up in increments of 1.
I've created a custom Y axis, which works, but if I set the maximum value greater than about 4.5, it cuts off the tickmark at the top of the axis. This looks untidy so I want a way to ensure this tickmark will always appear, but I don't want to shorten my axis as it looks stupid if I do this.
My R code is as follows:
# Bar plot of mean SUS question scores
barplot(meanSUSQuestions$Mean,
main="Mean SUS Question Scores",
cex.main="0.8",
cex.axis="0.8",
cex.lab="0.8",
#names=c("q1", "q2", "q3","q4","q5","q6","q7","q8","q9","q10"),
names=c(1:10),
yaxt="n",
col="red")
axis(2, cex.axis="0.8", at=seq(0, 5, 0.5)) # Create custom Y axis
mtext(text="Mean Score", side=2, line=2, cex=0.8)
mtext(text="Question", side=1, line=2, cex=0.8)
The bar plot that this produces looks like this:
As you can see from the picture, the top tickmark is missing.
How can I get this top tickmark to appear?
barplot generates the image height based on the data. The range of your manual y-axis is considerably larger than the plot area and is thus cut off.
The easiest way to solve the issue in your specific case is to add an yaxp = c(0, 5, 11) to barplot instead of yaxt = "n" and axis.
A self-contained example:
# Bad
x <- 1:5
barplot(x, yaxt = "n") #, add = TRUE)
axis(2, at = seq(0, 6, 2)) # Create custom Y axis
# Good
barplot(x, yaxp = c(0, 6, 2))
Sorry if the question is quite naive but I am stuck and could not find a specific answer elsewhere. I am a beginner. I cannot make the vertical axis show in the plot explained below.
barplot(-(seq(0,10,2)), width=0.6, space=1, xlim=c(-12,12), ylim=c(2,10), horiz=T, axisnames=FALSE,col="green")
barplot(seq(0,10,2),add=T, width=0.6, space=1, axes=F,horiz=T,axisnames=FALSE,col="darkred")
When I try to "include the option axis.lty=1 to draw it" (quoted from statmethods.net) I get warning saying it is "no graphical parameter". Please could anyone enlighten me in using boxplot() to solve this? Thanks in advance.
see the manual of barplot:
axisnames logical. If TRUE, and if there are names.arg (see above),
the other axis is drawn (with lty = 0) and labeled.
axis.lty the graphics parameter lty applied to the axis and tick
marks of the categorical (default horizontal) axis. Note that by
default the axis is suppressed.
That axis will be drawn only when the height parameter has names attributes or you supplied names.arg. When this criteria was satisfied, a blank axis line will be drawn, and you can make it visible with axis.lty = 1. See the following example (the horizontal axis overlapped the first bar because you set ylim to c(2, 10)):
barplot(
-(seq(0,10,2)), width=0.6, space=1, names.arg = paste('h=', -(seq(0,10,2))),
xlim=c(-12,12), ylim=c(2,10),
horiz=T, axisnames=T,col="green", axis.lty = 1, las = 1)
barplot(seq(0,10,2),add=T, width=0.6, space=1,
axes=F,horiz=T,axisnames=FALSE,col="darkred", axis.lty = 1)
I would like to realign/offset the x-axis and associated tick marks of a barplot. This should be simple but I am having trouble finding an answer. Below is some example data with 24 categories.
xval = c(1:24)
count = c(0.03,0.03,0.08,0.06,0.11,0.4,0.3,0.5,0.5,0.6,0.4,0.1,0.1,0.4,0.2,0.1,0.06,0.05,0.03,0.02,0.01,0.03,0.01,0.02)
df = as.data.frame(cbind(xval, count))
I can easily produce a barplot with tick marks aligned at the bar midpoints using the below code:
mp <- barplot(df$count, space=0, axes=FALSE)
axis(side=2, pos=-0.2)
axis(side=1, at =mp, labels=df$xval)
I can also shift the entire x-axis (labels and ticks) to align with the outside of bars using the below (although this now fails to incorporate the last bar into the axis):
axis(side=1, at =mp-0.5, labels=df$xval)
While I would like the x-axis and associated tick marks to be aligned with the bar boundaries (i.e. a tick mark on either side of the bar instead of in the centre), I want the x-axis labels to remain at the bar midpoints. Is there an easy way to achieve this?
Is this what you are looking for?
# create positions for tick marks, one more than number of bars
at_tick <- seq_len(length(count) + 1)
# plot without axes
barplot(count, space = 0, axes = FALSE)
# add y-axis
axis(side = 2, pos = -0.2)
# add x-axis with offset positions, with ticks, but without labels.
axis(side = 1, at = at_tick - 1, labels = FALSE)
# add x-axis with centered position, with labels, but without ticks.
axis(side = 1, at = seq_along(count) - 0.5, tick = FALSE, labels = xval)
In the following example, I plot a custom tick mark at .95 (edited to make labels horizontal as per Thomas' suggestion):
d = matrix(runif(40), ncol=4)
colnames(d) = c('a','b','c','d')
barplot(
d,
beside=T,
col=c('#CD4E3C', '#816DC3','#569340', '#A87929'),
ylim=c(0,1),
cex.axis=.80,
main= 'Title',
las=1
)
abline(h= 1:10/10, col = 'lightgray', lty=3)
axis(side=2, at=c(.95), cex.axis=.75, tck=-.01, las=1)
abline(h= .95, col = '#000000', lty=3)
Which gives:
My custom label is too close to the regular label (which I also need), and I'd like to bring the label closer to the tick mark. I looked through
help(par)
How might a bring that label closer to the axis?
EDIT:
Making the tick mark labels horizontal helped, but I'd still like to indent the label for .95 to reflect the shortened tick mark.
Quick solution is to put las=2 in both your barplot() and axis() calls to make labels horizontal and they'll be clearer.
EDIT: Use mtext instead of axis:
mtext("0.95",2,.5,at=.95,las=2,cex=.75)
How can I centre the labels on the x-axis to match up with the bars? Also, how can I position the x axis label further down so it it is not obscured by the x-axis labels? Thanks!
par(mar= c(15,4,4,2) + 0.1)
barplot(58:1,xaxt="n",xlab="",ylab="Frequency", col=gray(5:0/5))
axis(1, labels=FALSE)
text(1:58, par("usr")[3] - 0.25, srt = 90, adj = 1,
labels = rep("Long Species Name",58), xpd = TRUE)
mtext(1, text = "Species", line=6)
Check out the return value of barplot() (by reading ?barplot). There we find that the mid points of the bars are returned by the function as a vector. Hence it is a simple matter of assigning the returned object (here to object bar) and then use that in a call to axis() to locate the tick marks.
In the axis() call, note that we specify both the labels argument and the at argument, with at being set to the bar mid points as stored in bar. las = 2 is used to rotate the labels relative to the axis, and cex.axis = 0.6 is used to reduce the label size.
The second part of your question is handled by title() and the line argument. First note that when you set the mar parameter you are setting the margin size in "lines", hence the margin on side 1 (bottom) is 15 lines. The line argument in title() specifies which of the margin lines you want to draw the axis label.
Putting this altogether with a modified example we have:
op <- par(mar= c(15,4,4,2) + 0.1)
bar <- barplot(58:1, xaxt="n", xlab="", ylab="Frequency", col=gray(5:0/5))
axis(1, labels = paste("Long Species Name", 1:58), at = bar,
las = 2, cex.axis = 0.6)
title(xlab = "Species", line=11)
par(op)
Which produces: