Changing text size of 3d plot, R - r

I have a problem changing the text size of a 3d plot I generated with the package rgl. Everything works fine, but I can't effectively change the cex and size properties of an 3d object when rendering it in shiny, with the renderWebGL
library(rgl)
plot3d(x, y, z, xlab ="x", ylab ="y", zlab ="z")
texts3d(x, y, z, rownames(data))
Any help is highly appreciated! Best Regards.
Brecht

You can scale the text (including axis labels) by changing the "cex" rgl parameter, by calling the function par3d. The "cex" rgl parameter is different from the "cex" parameter in base graphics.
For example, if you want to magnify the text in your plot by a factor of 2, then you can call:
par3d(cex=2.0)
with(iris,
plot3d(Sepal.Length, Sepal.Width, Petal.Length,
type="s", col=as.numeric(Species)))
Note that calling par3d opens a plotting window. Calling a plotting function creates a plot in that same window. You have to call par3d every time you create a new plot. The function par3d can also change many other rgl parameters.

I ran into similar problems with library plot functions from PerformanceAnalytics. I would advise to get the code for the function by typing plot3d in the console and looking at the original plot functions inside.
It might be the case that the plot3d function doesn't pass a cex option forward, so you can also copy the function and modify it to make it your own myplot3d function or something like that.

For anyone still trying to address this issue, the plot3D package takes care of this nicely now.
See the following reproducible code.
# Load plot3D package
library(plot3D)
Using the iris dataset, let's make a 3D graph. See our use of cex, cex.main, and cex.lab at the end, which allow us to multiply text size, all within the package.
scatter3D(
x = iris$Sepal.Length,
y = iris$Sepal.Width,
z = iris$Petal.Length,
# Add a nice background
bty = "u",
col.grid = "darkgrey",
col.panel = "black",
# Specify Labels
main = "My graph",
clab = "Petal\nLength",
xlab = "\nSepal Length",
ylab = "\nSepal Width",
zlab = "\nPetal Length",
# Adjust label sizing
cex = 2, # Multiply dot size by 2
cex.main = 2, # Multiply the size of main title text by 2
cex.lab = 2) # Multiply size of axis label text by 2
Or, we can shrink the text size back to normal, here:
scatter3D(
x = iris$Sepal.Length,
y = iris$Sepal.Width,
z = iris$Petal.Length,
# Add a nice background
bty = "u",
col.grid = "darkgrey",
col.panel = "black",
# Specify Labels
main = "My graph",
clab = "Petal\nLength",
xlab = "\nSepal Length",
ylab = "\nSepal Width",
zlab = "\nPetal Length",
# Adjust label sizing
cex = 1, # Multiply dot size by 2
cex.main = 1, # Multiply the size of main title text by 2
cex.lab = 1) # Multiply size of axis label text by 2

Not the OP situation but if you need to change the text size on a rgl plot and your text is a LaTeX expression you have to pass the text size parameter (cex) to the corresponding function.
For example,
library(rgl)
library(magrittr)
iris %>%
{points3d('x'=.$Sepal.Length, 'y'=.$Sepal.Width, z=.$Petal.Length)}
title3d(main=NULL, sub=setName,
xlab=latex2exp::TeX(r"($\phi_{\pi})"),
ylab=latex2exp::TeX(r"($\phi_{X}$)"),
zlab=latex2exp::TeX(r"($\psi_{B}$)"),
cex=4)
Notice the cex argument in title3d() which is passed to plotmath3d() which actually draws the LaTeX expression into a PNG file that is then paste on the final plot. See the documentation for plotmath3d for details but cex is a number that scales the text size.

Related

How to make y axis labels horizontal but keep y-axis title parallel?

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:

Plotting in R using plot function

I am trying to plot few graphs using loops. I am now describing in details.
First I have a function which is calculates the y-variable (called effect for vertical axis)
effect<- function (x, y){
exp(-0.35*log(x)
+0.17*log(y)
-0.36*sqrt(log(x)*log(y)/100))
}
Now I run the following code and use the option par to plot the lines in the same graph. I use axis=FALSE and xlab="" to get a plot without labels. I do this so that my labels are not re-written each time the loop runs and looks ugly.
for (levels in seq(exp(8), exp(10), length.out = 5)){
x = seq(exp(1),exp(10), length.out = 20)
prc= effect(levels,x)
plot(x, prc,xlim = c(0,max(x)*1.05), ylim=c(0.0,0.3),
type="o", xlab = "",ylab = "", pch = 16,
col = "dark blue", lwd = 2, cex = 1, axes = F)
label = as.integer(levels) #x variable
text(max(x)*1.03,max(prc), label )
par(new=TRUE)
}
Finally, I duplicate the plot command this time using the xlab and ylab options
plot(x, prc, xlab = "X-label", ylab = "effect",
xlim = c(0,max(x)*1.05), ylim = c(0,0.3),
type="l", col ='blue')
I have several other plots in the similar lines, using complex equations. I have two questions:
Is there an better option to have the same plot with smoother lines?
Is there an easier option with few lines to achieve the same, where I can place the texts (levels) for each line on the right with white background at the back?
I believe working with the plot function was tedious and time consuming. So, I have finally used ggplot2 to plot. There were several help available online, which I have used.

Control axis label size when plotting igraph object in R

Is there any possibility to control for the font size of axis labels when plotting a network generated with igraph in R?
Contrary to the package "network", cex.lab doesn't change anything when passed to 'plot'.
An example:
library(igraph)
testnet <- graph_from_adjacency_matrix(diag(10))
plot(testnet,xlab="This is xlab Text")
par(mfrow=c(2,3))
replicate(6,plot(testnet,xlab="This is xlab Text"))
replicate(6,plot(testnet,xlab="This is xlab Text",cex.lab=10))
With multiple plots on one page, the default font size is too small.
Note that I am not talking about setting the size of vertex labels (vertex.label.cex).
This is a workaround to your problem (even if it is not the solution to it). One possibility to get what you want is to omit the x-label and add a customizable text below your plot.
par(mfrow=c(1,2))
# plot with x-axis label
plot(graph_from_adjacency_matrix(diag(10)), xlab = "mylab")
# plot w/o x-axis label + text
plot(graph_from_adjacency_matrix(diag(10)))
text(0, -1.8, labels = "myxlab", cex = 2.5)
This could be your result.
Another option: add the label size to par.
One size for all plots
rr <- 2; cc <- 3
par(mfrow=c(rr,cc), cex.lab=1.5)
replicate(6,plot(testnet,xlab="This is xlab Text"))
Different sizes for the plots:
par(mfrow = c(rr,cc))
plot.new()
cex.labs <- matrix(runif(2*3, 1, 3), ncol=cc, nrow=rr)
for (x in seq_len(rr))
for (y in seq_len(cc)) {
par(mfg=c(x,y), cex.lab = cex.labs[x,y])
plot(testnet,xlab="This is xlab Text")
}

RStudio increase font size in a barplot graphic

I am running R withing RStudio (Windows 10 OS) and have used the barplot function to make a plot.
The default label size looks fine but I need to send the file through a conversion package for a journal and in the converted file the resulting axis labels are too small. Thus, I seek R to increase the font size of the labels in the hope that the converted file will have a larger font size for the labels.
The two bar plots below yield the same size label fonts. Thus, adding "cex.names = 3" to the second bar plot has no effect.
barplot(temp$somenumber,
xlab = "My X label", ylab = "My Y label")
barplot(temp$somenumber,
xlab = "My X label", ylab = "My Y label", cex.names = 3)
Any guidance would be appreciated.
Use cex.lab=2. But you may need to set mar to avoid
You can see that there is not enough space for y lable.
From ?par
mar: A numerical vector of the form c(bottom, left, top, right) which gives the number of lines of margin to be specified on the four sides of the plot. The default is c(5, 4, 4, 2) + 0.1.
old.par <- par(mar = c(5,8,4,2)+0.1)
barplot(VADeaths, xlab = "My X label", ylab = "My Y label", cex.lab = 2)
par(old.par)
After I set mar[2] to 8.1 instead of 4.1, I can get
Good luck to you!

Avoid overlapping axis labels in R

I want to plot data in a graph with larger font-size for the lables.
x = c(0:10)
y = sin(x) + 10
plot (
x, y, type="o",
xlab = "X values",
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
Unfortunately the numbers on the y-axis overlap the label for the y-axis. I tried to use mar, but that did not work (By the way, how can I find out which graphic parameters can be directly used in the plot command and which have to be set with the par()-method? ).
How can I avoid that labels overlap?
Thanks for your help.
Sven
Use par(mar) to increase the plot margins and par(mgp) to move the axis label.
par(mar = c(6.5, 6.5, 0.5, 0.5), mgp = c(5, 1, 0))
#Then call plot as before
In the help page ?par it explains which parameters can be used directly in plot and which must be called via par.
There are several parameters can only be set by a call to ‘par()’:
• ‘"ask"’,
• ‘"fig"’, ‘"fin"’,
• ‘"lheight"’,
• ‘"mai"’, ‘"mar"’, ‘"mex"’, ‘"mfcol"’, ‘"mfrow"’, ‘"mfg"’,
• ‘"new"’,
• ‘"oma"’, ‘"omd"’, ‘"omi"’,
• ‘"pin"’, ‘"plt"’, ‘"ps"’, ‘"pty"’,
• ‘"usr"’,
• ‘"xlog"’, ‘"ylog"’
The remaining parameters can also be set as arguments (often via
‘...’) to high-level plot functions such as ‘plot.default’,
‘plot.window’, ‘points’, ‘lines’, ‘abline’, ‘axis’, ‘title’,
‘text’, ‘mtext’, ‘segments’, ‘symbols’, ‘arrows’, ‘polygon’,
‘rect’, ‘box’, ‘contour’, ‘filled.contour’ and ‘image’. Such
settings will be active during the execution of the function,
only. However, see the comments on ‘bg’ and ‘cex’, which may be
taken as _arguments_ to certain plot functions rather than as
graphical parameters.
The quick and dirty way would be to use par and add a newline in ylab, even though it's conceptually terrible.
x = 0:10
y = sin(x) + 10
par(mar=c(5,7,4,2))
plot (
x, y, type="o",
xlab = "X values",
ylab = "Y values\n",
cex.axis = "2",
cex.lab = "2",
las = 1
)
Concerning which parameters you can set directly in plot have a look at ?plot.default and ?plot.xy as they will recieve the ... arugments. There's also a couple of calls to undocumented functions (as far as I can find) like localWindow and localBox but I don't know what happens to them. I'd guess they're just ignored.
You can put the mgp parameter into the title() function to avoid having to reset your defaults afterwards. That way the parameter only acts on the label(s) added by the function. like this:
plot (
x, y, type="o",
xlab = "", #Don't include xlab in main plot
ylab = "Y values",
cex.axis = "2",
cex.lab = "2",
las = 1
)
title(xlab="X values"
,mgp=c(6,1,0)) #Set the distance of title from plot to 6 (default is 3).

Resources