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:
Related
I would like to make my y axis labels horizontal, while keeping my y axis titles as parallel.
When I try inputting las=1 into the twoor.plot()argument, nothing happens. I have also tried ylas=1, y_las=1, lylas=1, rylas=1, and nothing happens. The only way I've been able to make my yaxis labels horizontal, is by using par(las=1), but then this makes my y-axis titles horizontal too, which I don't want...
This is my code so far:
par(las=1)
yFrequency <- c(0,20,40,60,80,100,120,140,160)
GS_class_labels <- c("<2", "2-4", "4-8", "8-16", "16-32", "32-64", "64-128", "128<")
twoord.plot(data=distribution,lx="Var1",ly="Freq", ry="cum_percentile",
main="B1 Surface Grain Size Distribution",
xlim=NULL,lylim=c(0,160),rylim=NULL,lwd=1.5,
lcol=1,rcol=2,xlab="Grain Size (mm)",lytickpos=yFrequency,
ylab="Frequency",ylab.at=NA,
rytickpos=NA,rylab="Percent Finer Than (%)",rylab.at=NA,
lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=GS_class_labels,
halfwidth=0.4,axislab.cex=1.1,
do.first=NULL,xaxt="s", yticklab=yFrequency, cex.lab=1)
An alternative way to set the y axis labels parallel is as follows.
(1) Set both of the ylab and rylab from twoord.plot to empty.
(2) Use mtext and set the parameters accordingly.
Here is the code to do that. Because you don't provide the distribution data, I use iris data just to make it possible to generate the plot.
# Emptying both of ylab and rylab
twoord.plot(data = iris,lx="Sepal.Length",ly="Petal.Width", ry="Sepal.Width",
main="B1 Surface Grain Size Distribution",
xlim=NULL,lylim=c(0,160),rylim=NULL,lwd=1.5,
lcol=1,rcol=2,xlab="Grain Size (mm)",lytickpos=yFrequency,
ylab="",ylab.at=NA,
rytickpos=NA,rylab="",rylab.at=NA,
lpch=1,rpch=2,
type="b",xtickpos=NULL,xticklab=GS_class_labels,
halfwidth=0.4,axislab.cex=1.1,
do.first=NULL,xaxt="n",yaxt="n", #yticklab=yFrequency,
cex.lab=1)
# Assign the previous labels of ylab and rylab to the *text* parameter of *mtext*.
# side = 2 means the left side. side = 4 means the right side.
# las = 0 is the parallel style of the text.
# line shows the distance of the text from the y axis.
mtext(text = "Frequency", side = 2, las = 0, line = 2.5)
mtext(text = "Percent Finer Than (%)", side = 4, las = 0, line = 0.5)
The resulted plot:
I have this code:
# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n") # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks) # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow") # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Which produces this graph:
And I can't seem to figure out how to get the secondary y-axis label to show up in the correct position. Any help or suggestions is greatly appreciated.
Edit: Using RStudio.
One option is to specify the line argument to mtext(). In the example below I add a couple more lines to the right (side = 4) margin of the plot using par(), and then I draw three labels using mtext() at the default (line = 0), line 3 (line = 3), and line -3 (line = -3):
op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Note that line numbers increase away from the plot region and that negative line values move into the plot region, or to the left of the right boundary of the plot region.
It takes a little playing with the number of margin lines (as set in par(mar = x)) and which line you want to draw on using mtext(), but a little trial and error should get you what you want.
Note also that you don't need to specify integer values for the line argument. You can specify fractions of lines too: line = 2.5.
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)
I am creating a boxplot in R with the following code:
boxplot(perc.OM.y ~ Depth, axes = F, ylim = c(-0.6, 0.2), xlim = c(3.5, 5.5),
lwd = 0.1, col = 8,
ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5)
axis(1, at = c(3.5, 4, 5, 5.5), labels = c(" ", "Shallow", "Deep", " "),
cex.axis = 1.5)
axis(2, cex.axis = 1.5)
The problem is that the number labels on the y-axis currently overlap the axis title. Is there a way to put more space between the axis title and the axis number labels?
Thanks
## dummy data
dat <- data.frame(Depth = sample(c(3:6), 20, replace = TRUE), OM = 5 * runif(20))
Add some room for the y-axis labels and annotations, by making the margin bigger on the left hand side of the plot (side = 2):
## margin for side 2 is 7 lines in size
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1
Now plot:
## draw the plot but without annotation
boxplot(OM ~ Depth, data = dat, axes = FALSE, ann = FALSE)
## add axes
axis(1, at = 1:4, labels = c(" ", "Shallow", "Deep", " "), cex.axis = 1.5)
axis(2, cex.axis = 2)
## now draw the y-axis annotation on a different line out from the plot
## using the extra margin space:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
## draw the box to finish off
box()
Then reset the plotting margins:
par(op)
This gives:
So we've created more space for the margin of the plot on side 2, and then drawn the axes and the annotation (ylab) separately to control how the plot is spaced out.
So the key to this is this line:
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1
What we do is save the original graphical parameters in object op, and change the margin sizes (in numbers of lines) to be 5, 7, 4, 2 + 0.1 lines each for the bottom , left, top, right margins respectively. The line above shows the defaults, so the code gives 2 more lines on the left margin than usually provided by default.
Then when we draw the y-axis label using title(), we specify which line (of the 7) to draw the label at:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
Here I used line 4.5, but 5 would work also. The greater the values of 'line' the farther from the plot the label is drawn.
The trick is to find the value for the left margin and the value of 'line' in the title() call that allows the axis tick marks and the axis label to not overlap. Trial and error is likely the best solution to find the values you need with base graphics.
Try setting the first value of mgp larger. You'll want to make the margins bigger too, with mar.
par(mgp=c(5,1,0))
par(mar=c(5,6,4,2)+0.1)
I just found this solution very straightforward and useful when I wanted to shrink the white space around the diagram (consider size limits in the conference papers!) while I wanted to avoid overlapping Y-axes title and big numbers as the ticks.
I use to set the titles as text and put them wherever I want, after setting the margins manually:
First, set the margins to the arbitrary values:
par( mar=c(m1, m2, m3, m4) )
where m1 to m4 are margins for four sides (1=bottom, 2=left, 3=top and 4=right).
For example:
par( mar=c(3.1, 4.7, 2.3, 0))
Then, when plotting, set main="", xlab="" and ylab="" (otherwise their text will overlap with this new text)
Finally, using mtext(), set the axis titles and diagram title manually:
mtext(side=1, text="X axes title", line=0.5)
mtext(side=2, text="Y axes title", line=3)
mtext(side=3, text="Diagram title", line=1.5)
The line parameter is the distance from the diagram (the smaller values puts it closer to the diagram).
I'm trying to move the x-axis labeling and tick marks above the plot on top. Here's my code.
ucsplot <- plot(ucs, depth, main= "Depth vs. UCS", xlab = "UCS (psi)", ylab="Depth (ft)", type="l", col="blue", xlim=c(0, max(dfplot[,3]+5000)), ylim = rev(range(depth)))
ucsplot
How do I get the x-axis labeling and tick marks to appear only on top, instead of the bottom? Also, how do I get the title to not sit right on top of the numbers right above the tick marks? Also, how do I get the chart to start not offset a little bit to the right? As in the zero and starting numbers are in the corners of the plot and not offset.
Seems the OP is looking for a plot where x-axis is at top. The data has not been provided by OP. Hence using a sample dataframe, solution can be displayed as:
df <- data.frame(a = 1:10, b = 41:50)
plot(a ~ b, data = df, axes = FALSE, xlab = NA, ylab = NA)
axis(side = 2, las = 1)
axis(side = 3, las = 1)