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)
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))
i've got a tiny problem in here, which i would like to have some hints on.
How can i change the space between ticks and labels? (indicated with 1 & 2)
my current structure looks as follows:
par(mfrow=c(5,2),oma=c(0,0,2,0),las=1,mar=c(3,5,2,1),cex.lab=0.9, cex.axis=0.7)
plot(sapply(ERRORS.train.fast[[1]],mean),main="Pipe 63569",type="l", ylab="", xlab="",xaxt="n")
axis(1, at=1:29,labels=seq(2,30,1))
title(ylab= "RMSE (-)",line=3)
title(xlab= "K-Value",line=2)
highly appreciate your help!
cheers,
Olli
You can use the padj argument for "adjustment for each tick label perpendicular to the reading direction." (from ?axis)
par(mfrow = c(1, 2))
plot(1:5, axes = F)
axis(1)
plot(1:5, axes = F)
axis(1, padj = -.75)
Unfortunately, the directions are different for the different axes (because it is relative what is "up" for the text), so to move the labels closer to the ticks, you will want lower padj values for the horizontal axis, but higher padj values for the vertical axis.
If you rotate the labels (as shown in your example plot on the vertical axis), you will use hadj instead of padj. Overall, I would expect you want something like:
plot(1:5, axes = F)
axis(1, padj = -.75)
axis(2, hadj = 0, las = 1)
You can use the mpg par.
par(mfrow=c(1,2))
plot(iris[,3:4], pch=20, col=rainbow(3)[iris$Species],
ylab="", xlab="",xaxt="n")
axis(1, at=1:7)
plot(iris[,3:4], pch=20, col=rainbow(3)[iris$Species],
ylab="", xlab="", xaxt="n")
axis(1, at=1:7, mgp=c(0,0.5,0))
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)
I am finishing with my script, but I cannot set an angle for x labels. I would like to use it for my data in specific index position:
INPUT:
xlabel <- (0,100,200,250,336)
xlabel.popis <- ("TATA","MAMA","OND","KOKO","LOLO")
OUTPUT:
Will be plotted xlabel.popis on specific xlabel postion on x axis (x axis is index line (0..500)) and xlabel.popis will have vertical rotation.
I tried:
plot(read.table(files2[i],header=F,sep="\t")$V7,main=file_bez2[i], axes=FALSE)
xlabel <- (0,100,200,250,336)
xlabel.popis <- ("TATA","MAMA","OND","KOKO","LOLO")
axis(1, at=seq_along(xlabel),labels=as.character(xlabel.popis, las=2, cex.label=90))
or I tried no axis but mtext(as.character(xlabel.popis),side=1,line=1.1,at=xlabel,srt=90)
Nothing worked, could you help me, It will be better for me with axis definition.And par() definiton did not work too.
Thank you so much
I believe you need to add labels with the text function after a call to axis.
example:
xlabel <- c(0,100,200,250,336)
xlabel.popis <- c("TATA","MAMA","OND","KOKO","LOLO")
plot(range(xlabel), c(1,1), t="l", xaxt="n", xlab="")
axis(1, at=xlabel, labels=FALSE)
text(x=xlabel, y=par()$usr[3]-0.1*(par()$usr[4]-par()$usr[3]),
labels=xlabel.popis, srt=45, adj=1, xpd=TRUE)
If you just want 90° rotation, consider the las argument:
plot(range(xlabel), c(1,1), t="l", xaxt="n", xlab="")
axis(1, at=xlabel, labels=xlabel.popis, las=2)
I have eliminated labels on the y axis because only the relative amount is really important.
w <- c(34170,24911,20323,14290,9605,7803,7113,6031,5140,4469)
plot(1:length(w), w, type="b", xlab="Number of clusters",
ylab="Within-cluster variance",
main="K=5 eliminates most of the within-cluster variance",
cex.main=1.5,
cex.lab=1.2,
font.main=20,
yaxt='n',lab=c(length(w),5,7), # no ticks on y axis, all ticks on x
family="Calibri Light")
However, suppressing those tick labels leaves a lot of white space between the y axis label ("Within-cluster variance") and the y axis. Is there a way to nudge it back over? If I somehow set the (invisible) tick labels to go inside the axis, would the axis label settles along the axis?
Try setting ylab="" in your plot call and use title to set the label of the y-axis manually. Using line you could adjust the position of the label, e.g.:
plot(1:length(w), w, type="b", xlab="Number of clusters", ylab="",
main="K=5 eliminates most of the within-cluster variance",
cex.main=1.5,
cex.lab=1.2,
font.main=20,
yaxt='n',lab=c(length(w),5,7), # no ticks on y axis, all ticks on x
family="Calibri Light")
title(ylab="Within-cluster variance", line=0, cex.lab=1.2, family="Calibri Light")
Please read ?title for more details.
Adjust mgp, see ?par
title(ylab="Within-cluster variance", mgp=c(1,1,0), family="Calibri Light",cex.lab=1.2)