cex.axis only affects y-axis, not x-axis - r

I made a plot of the factor variable "answer" in subset A1. I want to decrease the text size on both axes, in order to fit in both extreme values on the x-axis. However, when using cex.axis, only the font size on the y-label is affected, not on the x-axis. Why is that, and how can I change that?
The function I used is:
plot(A1$answer, main = "Would you recommend edX to a friend of you?", xlab = "Answer", ylab = "#students", col='lightblue', cex.axis=0.75, font=3, family='mono'); box(col='lightblue');
And this is the output:

When you use plot on a factor variable x it calls barplot by default (or to be more precise barplot(table(x)), i.e you can look into ?barplot for hints. In this case, as I mentioned in the comments, the x-axis is considered as labels, not a numeric axis, therefore you need to use cex.names like so:
tab <- as.ordered(sample(1:10, 100, replace = TRUE))
plot(tab, cex.axis = 0.75, cex.names = 0.75)
Also, as hinted above, if you want to use barplot directly, you need to make a table first
barplot(table(tab), cex.axis = 0.75, cex.names = 0.2)

I just stumbled upon the barchart function in lattice, and this has a much better output:

Related

Why is `ann=FALSE` not working in the boxplot call in R?

Trying to produce both a stripchart and a boxplot of the same (transformed) data but (because the boxplot is shifted down a tad) I don't want the axis labels twice:
set.seed(3121975)
bee = list(x1=rnbinom(50, mu = 4, size = .1),
x2=rnbinom(30,mu=6,size=.1),
x3=rnbinom(40,mu=2,size=.1))
f = function(x) asinh(sqrt(4*x+1.5))
stripchart(lapply(bee,f),method="stack",offset=.13,ylim=c(.8,3.9))
boxplot(lapply(bee,f),horizontal=TRUE,boxwex=.05,at=(1:3)-.1,add=TRUE,ann=FALSE)
Other things that don't work include: (i) leaving ann to take its default value of !add, (ii) specifying labels for ylab.
I presume I have missed something obvious but I am not seeing what it might be.
Just add yaxt = 'n' into boxplot() to suppress plotting of the y-axis. The argument ann controls axis titles and overall titles, not the axis itself.

R histogram missing negative x labels

I am trying to generate a histogram in R, but some of the x labels are missing.
Here is the code I wrote:
> tmp <- hist(x, breaks=-3.5:(max(x)+1), xaxt="n", right=FALSE, xlab="log(MRS)",main="Pairwise Family-Health")
> axis(1, at=tmp$mids, labels=-3.5:max(x))
(The x values should be -3.5,-2.5,-1.5,-0.5, 0.5, 1.5, 2.5, 3.5, and the bars centering at these values.)
Does anyone know what the problem might be? Thanks!
In addition to the methods suggested in the comments (making the plot bigger, or making the text smaller), you may also want to look at the staxlab function in the plotrix package which automates a stacked label approach (puts the labels alternating on 2 or more lines to include more labels but still prevent overlap).
Try using the gap.axis argument within the axis function. So something like,
axis(side = 1, at = seq(-500000, 500000, by = 100000), gap.axis = 0.25)`
helped me when I had the same problem.

Set the number of divisions on plotting axes [duplicate]

I am creating a plot in R and I dont like the x axis values being plotted by R.
For example:
x <- seq(10,200,10)
y <- runif(x)
plot(x,y)
This plots a graph with the following values on the X axis:
50, 100, 150, 200
However, I want to plot the 20 values 10,20, 30 ... 200 stored in variable x, as the X axis values. I have scoured through countless blogs and the terse manual - after hours of searching, the closest I've come to finding anything useful is the following (summarized) instructions:
call plot() or par(), specifying argument xaxt='n'
call axis() e.g. axis(side = 1, at = seq(0, 10, by = 0.1), labels = FALSE, tcl = -0.2)
I tried it and the resulting plot had no x axis values at all. Is it possible that someone out there knows how to do this? I can't believe that no one has ever tried to do this before.
You'll find the answer to your question in the help page for ?axis.
Here is one of the help page examples, modified with your data:
Option 1: use xaxp to define the axis labels
plot(x,y, xaxt="n")
axis(1, xaxp=c(10, 200, 19), las=2)
Option 2: Use at and seq() to define the labels:
plot(x,y, xaxt="n")
axis(1, at = seq(10, 200, by = 10), las=2)
Both these options yield the same graphic:
PS. Since you have a large number of labels, you'll have to use additional arguments to get the text to fit in the plot. I use las to rotate the labels.
Take a closer look at the ?axis documentation. If you look at the description of the labels argument, you'll see that it is:
"a logical value specifying whether (numerical) annotations are
to be made at the tickmarks,"
So, just change it to true, and you'll get your tick labels.
x <- seq(10,200,10)
y <- runif(x)
plot(x,y,xaxt='n')
axis(side = 1, at = x,labels = T)
# Since TRUE is the default for labels, you can just use axis(side=1,at=x)
Be careful that if you don't stretch your window width, then R might not be able to write all your labels in. Play with the window width and you'll see what I mean.
It's too bad that you had such trouble finding documentation! What were your search terms? Try typing r axis into Google, and the first link you will get is that Quick R page that I mentioned earlier. Scroll down to "Axes", and you'll get a very nice little guide on how to do it. You should probably check there first for any plotting questions, it will be faster than waiting for a SO reply.
Hope this coding will helps you :)
plot(x,y,xaxt = 'n')
axis(side=1,at=c(1,20,30,50),labels=c("1975","1980","1985","1990"))
In case of plotting time series, the command ts.plot requires a different argument than xaxt="n"
require(graphics)
ts.plot(ldeaths, mdeaths, xlab="year", ylab="deaths", lty=c(1:2), gpars=list(xaxt="n"))
axis(1, at = seq(1974, 1980, by = 2))

Controlling axis ticks and axis lines separately on R lattice xyplot

How can I go about removing the box around an xyplot, while keeping the axis scale tick marks? In the spirit of Edward Tufte's minimalist data graphic aesthetic, these axis lines are "non-data ink," and can (should?) be "erased."
library(lattice)
my.df <- data.frame(x=-10:10)
my.df$y <- my.df$x^2
xyplot(y~x,data=my.df)
It seems that the trellis display parameters (e.g. axis.line$col) control both the axis lines and axis ticks together:
xyplot(y~x,data=my.df,
par.settings=list(axis.line=list(col="transparent")))
...which is not the desired result, so it doesn't look like there's a simple way to turn off the lines while leaving the box.
The best I've been able to come up with is a brute-force hack, where I build the tick marks by hand using panel.segments:
at.x=pretty(c(-10,10))
at.y=pretty(c(0,100))
xyplot(y~x,data=my.df,
par.settings=list(axis.line=list(col="transparent")),
scales=list(x=list(at=at.x,labels=at.x),
y=list(at=at.y,labels=at.y)),
panel=function(...){
panel.xyplot(...)
panel.segments(x0=at.x,x1=at.x,y0=-4,y1=-2)
panel.segments(x0=-11.5,x1=-11,y0=at.y,y1=at.y)
}
)
This is close to the desired result, but there's quite a bit of fiddling required to get the tick marks to be a reasonable length and offset a "nice" distance from the data points. These values won't translate from one graphic to the next. Plus, note that the axis labels are now padded too far from the tick marks. I'm sure there's a way to reduce that padding, but that would only make the code even uglier and less portable.
So how can one go about suppressing just the lines that make up the "box" around the plot area, while leaving the tick marks and axis labels intact? Bonus points if this approach could also be used to suppress some, but not all of the lines (e.g. leave the left and lower lines, but suppress the top and right lines).
This is still a bit hacky, but at least you don't have to do any figuring by hand. It uses a combination of par.settings and a custom axis function that takes an argument line.col and temporarily changes the axis line color by a call to trellis.par.set:
EDIT (removed unnecessary changing of trellis settings)
xyplot(y~x,data=my.df, par.settings = list(axis.line = list(col = "transparent")),
# Pass custom axis function to argument 'axis'
axis = function(side, line.col = "black", ...) {
# Only draw axes on the left and bottom
if(side %in% c("left","bottom")) {
# Call default axis drawing function
axis.default(side = side, line.col = "black", ...)
}
}
)
At the moment, I chalk up why line.col = "black" is required in the arguments of the custom axis function to magic. My guess is that it has to do with argument matching with the ellipses (...). Perhaps I'll be wiser tomorrow and find the true reason.
This results in:
The easiest thing to do is to use the custom axis function (axis). Just set lwd (line width) to zero and tick marks (lwd.ticks) to something else. It worked like a charm!
plot(NA,NA,type="n",xaxt="n", lwd=linewidth, xlim=c(1,24), xlab="", ylab="",ylim=c(-300,500))
axis(side = 4, tck = .05, **lwd=0, lwd.ticks=1**, line = 0, labels = NA, col= cols_border[1], col.axis = cols_black)
axis(side = 4, lwd = 0, line = -4.5, las = 1, cex.axis=axis_fontsize, col= cols_border[1], col.axis = cols_black)
mtext("Light deviations (lum/sec)",side=4, padj=-2.5, cex=title_fontsize, col="black")

Pass current cex.lab value to another function; Specify distance between lines of texts

Question 1: Suppose I have a function like the one below, which allows me to plot lines and also print multiple lines of texts below the x-axis (Sorry that it is the simplest code that I could come up with. I am open to any suggestions on how to make it neater!). When there is more than one line of text below the x-axis, it will probably overlap with the x-axis label. So I decided to use mtext() to plot the x-axis label.
Sampleplot = function(x, text, xlab = NULL,...){
if(!is.null(text)){
text.n=length(text) #Number of lines of text
text.n2=length(text[[1]]) #Number of words. Here I am assuming equal number in
#each list item
text.n=rep(1:text.n, each=text.n2)
matplot(x, axes = FALSE,...)
axis(2)
axis(1, labels = FALSE,at = 1:text.n2)
args.mtext=c(mtext, list(text=unlist(text),
line=text.n, at=1:text.n2, side=1))
mtext(xlab, line=max(text.n+1.5), side=1)
invisible(do.call(mapply, args.mtext))
}else matplot(x,...)
}
#Dataset and texts
dataset = cbind(sort(rlnorm(4)),sort(rlnorm(4)),sort(rlnorm(4)))
texts = list(letters[1:4], 1:4, letters[11:14])
#Sample plot:
par(mar=c(10,6,4,2))
Sampleplot(dataset, texts, xlab = "This is the label for x-axis",
type="b", pch=1)
Now, suppose I would like to increase the label size:
Sampleplot(dataset, texts, xlab = "This is the label for x-axis",
type="b", pch=1, cex.lab=3)
Of course, this is not going to do anything for the x axis label since it's created not using matplot() but using mtext().
One possible solution is to make cex.lab an explicit argument in the function, and also add it in matplot
Sampleplot2 = function(x, text, xlab = NULL, cex.lab = NULL, ...){ #<- HERE!!
if(is.null(cex.lab)) cex.lab = 1
if(!is.null(text)){
text.n=length(text) #Number of lines of text
text.n2=length(text[[1]]) #Number of words. Here I am assuming equal number in
#each list item
text.n=rep(1:text.n, each=text.n2)
matplot(x, axes = FALSE, cex.lab=cex.lab,...) #<- HERE!
axis(2)
axis(1, labels = FALSE,at = 1:text.n2)
args.mtext=c(mtext, list(text=unlist(text),
line=text.n, at=1:text.n2, side=1))
mtext(xlab, line=max(text.n+1.5), side=1, cex=cex.lab) #<- HERE!
invisible(do.call(mapply, args.mtext))
}else matplot(x,...)
}
par(mar=c(10,6,4,2))
Sampleplot2(dataset, texts, xlab = "This is the label for x-axis",
type="b", pch=1, cex.lab=2)
However, this solution seems far from elegant. So I wonder if there is any way to pass the cex.lab value WITHOUT having to explicitly put it in the function definition. I tried to use par()$cex.lab, but unlike par()$mar, which gives you the current margin status, par()$cex.lab only seems to give you the default label size, which is 1.
Question 2: As you can see in my plots, the spacing between the different lines of texts is kind of wide. I wonder if there is any way I can measure the height of the text, and I can use the height to perhaps decide how wide the space should be between different lines of texts.
Thank you guys in advance!

Resources