Label font size in PredictorEffects plot in R - r

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:

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

Add calculated mean value to vertical line in plot in R

I have created a density plot with a vertical line reflecting the mean - I would like to include the calculated mean number in the graph but don't know how
(for example the mean 1.2 should appear in the graph).
beta_budget[,2] is the column which includes the different numbers of the price.
windows()
plot(density(beta_budget[,2]), xlim= c(-0.1,15), type ="l", xlab = "Beta Coefficients", main = "Preis", col = "black")
abline(v=mean(beta_budget[,2]), col="blue")
legend("topright", legend = c("Price", "Mean"), col = c("black", "blue"), lty=1, cex=0.8)
I tried it with the text command but it didn't work...
Thank you for your advise!
Something along these lines:
Data:
set.seed(123)
df <- data.frame(
v1 = rnorm(1000)
)
Draw histogram with density line:
hist(df$v1, freq = F, main = "")
lines(density(df$v1, kernel = "cosine", bw = 0.5))
abline(v = mean(df$v1), col = "blue", lty = 3, lwd = 2)
Include the mean as a text element:
text(mean(df$v1), # position of text on x-axis
max(density(df$v1)[[2]]), # position of text on y-axis
mean(df$v1), # text to be plotted
pos = 4, srt = 270, cex = 0.8, col = "blue") # some graphical parameters

Padding Around Legend when using Pch in Base R

Just a minor question. I am trying to make a legend for the following plot.
# fitting the linear model
iris_lm = lm(Petal.Length ~ Sepal.Length, data = iris)
summary(iris_lm)
# calculating the confidence interval for the fitted line
preds = predict(iris_lm, newdata = data.frame(Sepal.Length = seq(4,8,0.1)),
interval = "confidence")
# making the initial plot
par(family = "serif")
plot(Petal.Length ~ Sepal.Length, data = iris, col = "darkgrey",
family = "serif", las = 1, xlab = "Sepal Length", ylab = "Pedal Length")
# shading in the confidence interval
polygon(
c(seq(8,4,-0.1), seq(4,8,0.1)), # all of the necessary x values
c(rev(preds[,3]), preds[,2]), # all of the necessary y values
col = rgb(0.2745098, 0.5098039, 0.7058824, 0.4), # the color of the interval
border = NA # turning off the border
)
# adding the regression line
abline(iris_lm, col = "SteelBlue")
# adding a legend
legend("bottomright", legend = c("Fitted Values", "Confidence Interval"),
lty = c(1,0))
Here's the output so far:
My goal is to put a box in the legend next to the "Confidence Interval" tab, and color it in the same shade that it is in the picture. Naturally, I thought to use the pch parameter. However, when I re-run my code with the additional legend option pch = c(NA, 25), I get the following:
It is not super noticeable, but if you look closely at the padding on the left margin of the legend, it actually has decreased, and the edge of the border is now closer to the line than I would like. Is there any way to work around this?
That's a curious behavior in legend(). I'm sure someone will suggest a ggplot2 alternative. However, legend() does offer a solution. This solution calls the function without plotting anything to capture the dimensions of the desired rectangle. The legend is then plotted with the elements you really want but no enclosing box (bty = "n"). The desired rectangle is added explicitly. I assume you mean pch = 22 to get the filled box symbol. I added pt.cex = 2 to make it a bit larger.
# Capture the confidence interval color, reusable variables
myCol <- rgb(0.2745098, 0.5098039, 0.7058824, 0.4)
legText <- c("Fitted Values", "Confidence Interval")
# Picking it up from 'adding a legend'
ans <- legend("bottomright", lty = c(1,0), legend = legText, plot = F)
r <- ans$rect
legend("bottomright", lty = c(1,0), legend = legText, pch = c(NA,22),
pt.bg = myCol, col = c(1, 0), pt.cex = 2, bty = "n")
# Draw the desired box
rect(r$left, r$top - r$h, r$left + r$w, r$top)
By the way, I don't think this will work without further tweaking if you place the legend on the left side.

Setting graph attributes in qqmath from the lattice package?

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

Change font-size in legend

I have a legend in my plot, but I'm trying to increase the font size so it fit the legend-box. When I try to increase the cex as defined below. The box gets bigger, while the text is still small.
Code:
legend(0,16, c("Available vCPUs", "Added vCPUs (1 per iteration ) "),
col=c('red', 'black'), cex=0.39, lty=1:1, lwd=2)
Excerpt from plot:
First approach:
Try to set the font size before to plot the legend.
x <- y <- rnorm(100, 0, 1)
plot(x, y, type = "n")
## here you set the font size default to `x`, in this example 0.5
## save defaults in `op`
op <- par(cex = 0.5)
legend("topright", legend = "foo legend", pch = 1, bty = "n")
## here you set cexto 1.5
## save new defaults in `op`
op <- par(cex = 1.5)
legend("topright", legend = "foo legend", pch = 1, bty = "n")
Second approach:
Holding the pt.cex parameter to 1, while trying different values for cex inside the legend call. Remember to delete op.
x <- rnorm(100, 10, 4)
y <- rnorm(100, 10, 4)
plot(x, y, type = "n")
## I tried to feed cex with 1.5 and 0.5. The font size changes while the points remain unchanged.
legend("topleft", "Legend", cex=0.5, pch=1, pt.cex = 1)
You can use cex to determine font size, use bty='n' to indicate no lines around the legend, then draw a rectangle separately on the graph with rect(). For example:
with(data, legend(-10,7, legend=c("Name_of_Legend"), bty = 'n', col=c("red"), lty=0, pch=20, cex=0.75))
with(data, rect(-10,6.2,-3,7))
I think you can try using the
y.intersp in legend, when the intervals between different text lines are reduced, you could increase text size without changing the size of legend box.
legend(0,16, c("Available vCPUs","Added vCPUs (1 per iteration )
"),col=c('red','black'),cex=0.39,lty=1:1,lwd=2, y.intersp = 0.3)

Resources