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")
}
Related
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:
Can I change the y-axis numbers to be horizontal on an NMDS plot created in vegan?
library(vegan)
sp <- poop[,28:34]
bat <- poop[,4:7]
mds1 <- metaMDS(sp, k=3,try=200)
plot(mds1$points[,1], mds1$points[,2], pch = as.numeric(bat$species),
col= as.numeric(bat$species),
xlab = "NMDS1", ylab= "NMDS2")
In R, the direction of labels is controlled by graphical parameter las (see ?par). You can also give this parameter in plot call for the metaMDS result. As you see from ?par, las=1 will put all labels horizontal.
More seriously, you should not plot metaMDS results like you do. It is better to use the dedicated plot method for the result, or if you want to do it all by yourself, you should at least force equal aspect ratio for axes with asp = 1 in your plot call. So the following should work:
## with metaMDS plot:
plot(mds1, display="si", las=1, type = "n") # for an empty plot
points(mds1, pch = as.numeric(bat$species), col= as.numeric(bat$species))
## or with generic plot:
plot(mds1$points[,1], mds1$points[,2], pch = as.numeric(bat$species),
col= as.numeric(bat$species),
xlab = "NMDS1", ylab= "NMDS2",
asp = 1, las = 1) # this is new
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!
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.
I am creating a boxplot in R with the following code:
boxplot(perc.OM.y ~ Depth, axes = F, ylim = c(-0.6, 0.2), xlim = c(3.5, 5.5),
lwd = 0.1, col = 8,
ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5)
axis(1, at = c(3.5, 4, 5, 5.5), labels = c(" ", "Shallow", "Deep", " "),
cex.axis = 1.5)
axis(2, cex.axis = 1.5)
The problem is that the number labels on the y-axis currently overlap the axis title. Is there a way to put more space between the axis title and the axis number labels?
Thanks
## dummy data
dat <- data.frame(Depth = sample(c(3:6), 20, replace = TRUE), OM = 5 * runif(20))
Add some room for the y-axis labels and annotations, by making the margin bigger on the left hand side of the plot (side = 2):
## margin for side 2 is 7 lines in size
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1
Now plot:
## draw the plot but without annotation
boxplot(OM ~ Depth, data = dat, axes = FALSE, ann = FALSE)
## add axes
axis(1, at = 1:4, labels = c(" ", "Shallow", "Deep", " "), cex.axis = 1.5)
axis(2, cex.axis = 2)
## now draw the y-axis annotation on a different line out from the plot
## using the extra margin space:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
## draw the box to finish off
box()
Then reset the plotting margins:
par(op)
This gives:
So we've created more space for the margin of the plot on side 2, and then drawn the axes and the annotation (ylab) separately to control how the plot is spaced out.
So the key to this is this line:
op <- par(mar = c(5,7,4,2) + 0.1) ## default is c(5,4,4,2) + 0.1
What we do is save the original graphical parameters in object op, and change the margin sizes (in numbers of lines) to be 5, 7, 4, 2 + 0.1 lines each for the bottom , left, top, right margins respectively. The line above shows the defaults, so the code gives 2 more lines on the left margin than usually provided by default.
Then when we draw the y-axis label using title(), we specify which line (of the 7) to draw the label at:
title(ylab = "Loss of Percent Organic Matter per Year", cex.lab = 1.5,
line = 4.5)
Here I used line 4.5, but 5 would work also. The greater the values of 'line' the farther from the plot the label is drawn.
The trick is to find the value for the left margin and the value of 'line' in the title() call that allows the axis tick marks and the axis label to not overlap. Trial and error is likely the best solution to find the values you need with base graphics.
Try setting the first value of mgp larger. You'll want to make the margins bigger too, with mar.
par(mgp=c(5,1,0))
par(mar=c(5,6,4,2)+0.1)
I just found this solution very straightforward and useful when I wanted to shrink the white space around the diagram (consider size limits in the conference papers!) while I wanted to avoid overlapping Y-axes title and big numbers as the ticks.
I use to set the titles as text and put them wherever I want, after setting the margins manually:
First, set the margins to the arbitrary values:
par( mar=c(m1, m2, m3, m4) )
where m1 to m4 are margins for four sides (1=bottom, 2=left, 3=top and 4=right).
For example:
par( mar=c(3.1, 4.7, 2.3, 0))
Then, when plotting, set main="", xlab="" and ylab="" (otherwise their text will overlap with this new text)
Finally, using mtext(), set the axis titles and diagram title manually:
mtext(side=1, text="X axes title", line=0.5)
mtext(side=2, text="Y axes title", line=3)
mtext(side=3, text="Diagram title", line=1.5)
The line parameter is the distance from the diagram (the smaller values puts it closer to the diagram).