Setting graph attributes in qqmath from the lattice package? - r

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)))

Related

Change size of the axis text in r

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))
)

Change the size of labels in mosaic function, R

I have a mosaic, and I want to change the size of the character of the labels. What can I do?
library(vcd)
tbl<-structable(GWage~Gender,data=dat)
mosaic(tbl,shade=TRUE, legend=TRUE)
Updated for Mosaic()
mosaic(UCBAdmissions, sort = 3:1,
gp_varnames = gpar(fontsize = 14, fontface = 1),
gp_labels = gpar(fontsize = 10),
main = "Student admissions at UC Berkeley")
?labelings
See ?labelings & play around with the font sizes till you get what you want. You should probably post a tibble /mini-extract of your data to make it easily reproducible.
you may be able to do more with mosaicplot()?
Did you try
cex.axis = 0.7 # or whatever size works for your plot?
#Example:
table1 <- table(airquality$Temp[1:7], airquality$Month[1:7])
mosaicplot(table1,
main = "Example",
xlab = "Y",
ylab = "X",
las = 2,
border = "chocolate",
cex.axis = 0.7,
shade = TRUE)

How to specify label length in R for axis parameter. My label names are truncated

In R, I've created a plot with a separate axis on the left to specify horizontal labels for the y-axis of the plot. However, the labels are truncated. That is, the complete name is not shown, only the last 9 characters of the name is shown. I use the R axis command to create the axis and the labels = names parameter to specify the names. names is a vector of character label names which vary in length.
plot(x = c(0,mx),y=c(1,n),yaxt = 'n', xlab = "Dollars - 100,000's", cex.axis = .65, typ = 'n', ylab = '', cex.lab = .8)
axis(side = 2, at = seq(1,n), labels = names, las = 2, cex.axis = .65)
Here's what the plot looks like:
Plot with truncated names
You can modify the margin of your plot by passing the mar argument to par function:
mar.default <- c(5,4,4,2) + 0.1
par(mar = mar.default + c(0, 4, 0, 0))
plot(x = c(0,mx),y=c(1,n),yaxt = 'n', xlab = "Dollars - 100,000's", cex.axis = .65, typ = 'n', ylab = '', cex.lab = .8)
axis(side = 2, at = seq(1,n), labels = names, las = 2, cex.axis = .65)
Without a reproducible example of your dataset, I can't guarantee that it will work right away, maybe you will have to adjust it on your own.
Otherwise, you can provide a reproducible example of your data: How to make a great R reproducible example

Manually set fontsize of axis titles in native-R plot and lattice graphing functions

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.

How do I plot a legend next to my title (outside plot) using R?

I'm using base R plot(), and I want a legend (a color block and key) to show up above (outside) the top right of my plot next to my title (generated using title()).
What's the best way to do this?
Maybe something like this is what you're looking for:
x <- c(1,2,3,4)
y <- c(4,1,3,2)
z <- c(1,2,3,4)
dat <- data.frame(x,y,z)
windows(width = 5, height = 9) #quartz() on Mac
layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(0.5,1))
par(oma = c(4,3,0,0) + 0.1, mar = c(0,0,1,1) + 0.1)
plot(dat$x, y=rep(1,4), type = "n", axes = F, ylab = "", xlab = "")
legend(x = "bottomright", legend = c("y", "z"), fill = c("blue", "red"))
plot(dat$x, dat$y, type = "n", main = "PLOT")
lines(z, col = "red")
lines(y, col = "blue")
Basically this makes two plots, one is just invisible and shortened so all that's displayed is the legend.
You may be able to addtionally tweak the margins around the legend and other graphical parameters (?par) to get the layout better.

Resources