Given a plot without axes, I would like to use axis to add a horizontal axis. On the left hand side you see the given "baseline" plot, on the right hand side the desired result:
The baseline plot is simply generated using
plot(1, axes = FALSE, main = "Baseline")
Question: How to generated the desired output? (Condition: using axis after the plot has been generated.)
I expected that I get what I want when specifying tick = FALSE, labels = FALSE, but this doesn't work. Below my tests, hopefully self-explanatory:
par(mfrow = c(2,2))
plot(1, axes = FALSE, main = "Full axis")
axis(1) # axis, ticks and labels
plot(1, axes = FALSE, main = "No labels")
axis(1, labels = FALSE) # axis, ticks
plot(1, axes = FALSE, main = "No ticks (and no axis)")
axis(1, tick = FALSE) # NO axis (unexpected), labels
plot(1, axes = FALSE, main = "Nothing (instead of axis)")
axis(1, tick = FALSE, labels = FALSE) # nothing - expected: only axis without ticks, without labels
I expected the last plot to deliver the desired output, but the axis is not drawn at all. Apparently, ticks = FALSE suppresses the axis itself as well.
Remark: The "expected" plot was generated using
plot(1, axes = FALSE, main = "Expected")
axis(1, at = c(-100, 100))
but I am looking for a "cleaner" solution than placing unwanted ticks outside the visible area.
Other workarounds are axis(1, lwd.tick=0, labels=FALSE), axis(1, tcl=0, labels=FALSE), which draw 0 width or 0 length ticks, respectively. #LyzandeR's solution seems to draw white ticks on top of the axis. The proposed box() solutions and ?par don't seem to draw just the X axis?
This seems to plot what you want:
plot(1, axes = FALSE, main = "No labels")
axis(1, label=F, col.ticks='white') # axis, ticks
It just prints the ticks as white:
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 want to omit the x-axis labels for my plot of means, but I just fail (I didn't use packages such as ggplot2):
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="")) # normal code
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",frame=F)) # which gives no response
with(richness_formok,plotMeans(barklicerare,invstatus,error.bars="conf.int", level=0.95, xlab="", ylab="", main="",axes=F))
This gives the following error:
Error in plot.default(c(1, n.levs), yrange, type = "n", xlab = xlab, ylab = ylab, :
formal argument "axes" matched by multiple actual arguments
The below code temporarily sets the color of axis tick labels (i.e., for both the x and y-axis) to "transparent" using par, thus preventing the automatically drawn tick labels from being visible. The y-axis tick labels can easily be inserted afterwards by reverting the par-related modifications.
## sample data
library(Rcmdr)
data(Moore)
## create plot with transparent (i.e., non-visible) tick labels
par(col.axis = "transparent")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
## add y-axis tick labels
par(col.axis = "black")
axis(side = 2, at = seq(0, 25, 5), tick = FALSE)
Remember that this is a rather undesired behavior of plotMeans since the help pages explicitly say that ... are arguments to be passed on to plot. Therefore, if your target is to entirely disable the drawing of the frame and axes when using plotMeans, e.g. similar to the visual output of plot(1:10, 1:10, axes = FALSE), you should probably file a bug report.
Update
In case I understood it wrongly and you actually wanted to remove both the tick labels and the ticks, you could simply use
par(xaxt = "n")
with(Moore, plotMeans(conformity, fcategory, partner.status,
ylim = c(0, 25)))
See also ?par for further possible modifications of R base plots.
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)
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:
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)