I have model created by train function from caret. I want to plot this object and increase size of text and title of axis. I found how to change size of titles, but I couldn't find how to do it for text on the axis. Example code for my problem below:
library(caret)
m <- train(mpg~., data = mtcars, tuneGrid = expand.grid(.mtry=c(2,4,5)))
plot(m, xlab = list(font=3, cex = 5), ylab = list(font=3, cex = 5))
I tried using cex.axis and ps parameters but none of them worked.
Adding the scales argument with a list for the x and y axes works for me. The items in the list for scales would be able to be customized like the axis labels were.
library(caret)
m <- train(mpg~., data = mtcars, tuneGrid = expand.grid(.mtry=c(2,4,5)))
plot(m, xlab = list(font=3, cex = 5),
ylab = list(font=3, cex = 5),
scales = list(x = list(font=2,cex=2),y=list(font=2,cex=2))
)
Related
I am plotting an ordinal regression plot with the PredictorEffects function, where I need to increase the font size of my labels. I was using cex function to do the same, but I cannot change the font size of x-axis.
Here are the codes:
m1 = polr(as.factor(views)~ caste, data = data, Hess=TRUE)
plot(predictorEffects(m1), lines=list(multiline=TRUE), xlab = list("Caste", cex = 2),
ylab= list("attitude probability", cex = 2), main=FALSE)
The cex function is working for y-axis but not for x-axis
Try using the cex.lab function:
The magnification to be used for x and y labels relative to the
current setting of cex.
Here an example no cex.lab:
x <- rnorm(100)
plot(x, main = "plot")
Output:
Here cex.lab=2:
plot(x, main = "plot", cex.lab = 2)
Output:
I'm trying to increase font size in mirt plots, however, so far I'm able to increase size of ticks only:
library(mirt)
x <- mirt(Science, 1, SE=TRUE)
plot(x)
plot(x, scales = list(cex = c(1.4)))
How can I increase size of the axis and main title? I tried to add xlab = list(cex = 1.4), however I'm getting an error:
Error in xyplot.formula(score ~ Theta, plt, ylim = c(sum(mins) - ybump_full, :
formal argument "xlab" matched by multiple actual arguments
**EDIT: **
Some parts can be increased with trellis.par.set() as suggested by #user20650, however it does not include font size of legend.
trellis.par.set(par.xlab.text = list(cex = 1.4), par.ylab.text = list(cex = 1.4))
plot(x, type = "trace", facet_items = FALSE, scales = list(cex = 1.4),
par.strip.text = list(cex = 1.4), main = FALSE)
Moreover, this does not have impact on the following plot:
plot(x, type = "infoSE", facet_items = FALSE, scales = list(cex = 1.4),
par.strip.text = list(cex = 1.4), main = FALSE)
You can set parameters globally with trellis.par.set or pass to the individual plot using the par.settings parameter. trellis.par.get() can be used to get a list of the names of the objects that can be updated.
So for example the following can be used to update specific parameters within a plot
plot(x, type = "trace",
par.settings=list(
par.xlab.text=list(cex=3, col="red"),
par.main.text=list(cex=2)))
Or to update globally use
trellis.par.set(par.xlab.text=list(cex=3, col="red"),
par.main.text=list(cex=2)
)
Using grid.pars=list(cex=3)) seems to update all text sizes
I am trying to prepare a graph for a poster presentation, but am getting very frustrated by how difficult things that should be simple are in plot. I want to plot a qq-plot of residuals from a mixed-effects model. All I want to do is change the font size of the axis title
. Here's a reproducible example.
library(lme4)
library(lattice)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
qqmath(fm1,
main = list("QQ-Plot", cex = 2),
id=0.05,
cex = list(x = 2),
scales = list(x = list(cex = 2), y = list(cex = 2)))
This all works fine. But when I try to increase the font size of the axis title
qqmath(fm1,
main = list("QQ-Plot", cex = 2),
xlab = list("x-axis", cex = 2),
id=0.05,
cex = list(x = 2),
scales = list(x = list(cex = 2), y = list(cex = 2)))
I get
Error in qqmath.formula(x = ~x, ylab = "Standardized residuals", xlab = "Standard normal quantiles", :
formal argument "xlab" matched by multiple actual arguments
I gather from this post that this is due to competing arguments in the function call and some ellipsis in the original qqmath.formula object, but surely there has to be an easier way to set the fontsize of the axis titles than reprogramming the original function?!
The lattice system has functions trellis.par.get and trellis.par.set and this can be used to control the fontsize of the xlab and ylab components:
?trellis.par.get
names( trellis.par.get() )
trellis.par.set(par.ylab.text=list(cex=.5))
qqmath(fm1,
main = list("QQ-Plot", cex = 2), id=0.05,
cex=list(left=.55,bottom=.5),
scales = list(x = list(cex = 1), y = list(cex = 1)))
... reduces the size of the ylab. You can find a more complete list of the components and features that can be set from a chart onpage 127 in the "Lattice" book by Sarkar.
Can anyone tell me how to set attributes within qqmath from the lattice package. Specifically how do I increase the font size of axis titles, axis text etc? None of the native plot arguments seem to work.
library(lattice)
lattice::qqmath(~ rnorm(100), distribution = function(p) qt(p, df = 10),
id=0.05,
main = "this is a title",
ylab = "this is a y-axis",
xlab = "this is an x-axis",
cex = 2,
cex.axis = 4,
font = 2,
cex.lab = 4)
Found a solution in the labyrinth that is the help page for lattice. In order to specify attributes of axis labels in lattice graphs you need to use the x and y argument within the scales argument, all wrapped in list(). To change the font size of titles you also need to wrap everything in a list():
lattice::qqmath(~ rnorm(100), distribution = function(p) qt(p, df = 10),
id=0.05,
main = list("this is a title", cex = 2),
ylab = list("this is a y-axis", cex = 3),
xlab = list("this is an x-axis", cex = 4),
scales = list(x = list(cex = 4)))
My GAM curves are being shifted downwards. Is there something wrong with the intercept? I'm using the same code as Introduction to statistical learning... Any help's appreciated..
Here's the code. I simulated some data (a straight line with noise), and fit GAM multiple times using bootstrap.
(It took me a while to figure out how to plot multiple GAM fits in one graph. Thanks to this post Sam's answer, and this post)
library(gam)
N = 1e2
set.seed(123)
dat = data.frame(x = 1:N,
y = seq(0, 5, length = N) + rnorm(N, mean = 0, sd = 2))
plot(dat$x, dat$y, xlim = c(1,100), ylim = c(-5,10))
gamFit = vector('list', 5)
for (ii in 1:5){
ind = sample(1:N, N, replace = T) #bootstrap
gamFit[[ii]] = gam(y ~ s(x, 10), data = dat, subset = ind)
par(new=T)
plot(gamFit[[ii]], col = 'blue',
xlim = c(1,100), ylim = c(-5,10),
axes = F, xlab='', ylab='')
}
The issue is with plot.gam. If you take a look at the help page (?plot.gam), there is a parameter called scale, which states:
a lower limit for the number of units covered by the limits on the ‘y’ for each plot. The default is scale=0, in which case each plot uses the range of the functions being plotted to create their ylim. By setting scale to be the maximum value of diff(ylim) for all the plots, then all subsequent plots will produced in the same vertical units. This is essential for comparing the importance of fitted terms in additive models.
This is an issue, since you are not using range of the function being plotted (i.e. the range of y is not -5 to 10). So what you need to do is change
plot(gamFit[[ii]], col = 'blue',
xlim = c(1,100), ylim = c(-5,10),
axes = F, xlab='', ylab='')
to
plot(gamFit[[ii]], col = 'blue',
scale = 15,
axes = F, xlab='', ylab='')
And you get:
Or you can just remove the xlim and ylim parameters from both calls to plot, and the automatic setting of plot to use the full range of the data will make everything work.