filled.contour() in R: labeling axis - cex, las, et al - r

I want to use filled.contour() to plot some data I have in a matrix.
Everything is perfect until when I import the graphics into my tex file and realize I need to play with the font size for it to be readable in the final document.
Unfortunately, it seems I am unable to tune the parameter cex in filled.contour(), and the same goes for las (I'd like the ylabel to be parallel to the x axis).
Below is a simple example. Although I expected the output to be different in each case, namely the font size, the produced plot is pretty much the same.
Thanks a lot for any help you can give me on this.
x=1:10
y=1:10
z=array(rnorm(100),dim=c(10,10))
filled.contour(x,y,z)
filled.contour(x,y,z,xlab='x',ylab='y')
filled.contour(x,y,z,xlab='x',ylab='y',las=1)
filled.contour(x,y,z,xlab='x',ylab='y',las=1,cex=2)
filled.contour(x,y,z,xlab='x',ylab='y',las=1,cex=20)

#QuantIbex is right, though you can also pass through other graphics parameters by specifying in the plot.title, plot.axes, key.title and key.axes arguments.
This is necessary because the usual graphics parameters are not passed straight through, as described in ?filled.contour:
...: additional graphical parameters, currently only passed to
‘title()’.
E.g.:
x=1:10
y=1:10
z=array(rnorm(100),dim=c(10,10))
filled.contour(x,y,z,las=0,
plot.axes={
axis(1,cex.axis=2)
axis(2,cex.axis=2)
},
plot.title={
title(xlab="x",cex.lab=2)
mtext("y",2,cex=2,line=3,las=1)
}
)

Related

R igraph output vertice is not shown

I am using R igraph package to display gene networks. The plot on Rstudio is like this (I can't post image because I am new user and don't have enough reputation, sorry about that):
R igraph on preview
Now I want to draw this on file to clearly see the changes and there is always an issue on vertices near margin side like this:
part of output pdf file
My code is as follows`
pdf("graph.pdf",width = 20, height = 10)
par(mar = c(9,9,9,9))
plot(finalnet, edge.arrow.size=0.1, edge.curved=FALSE,vertex.size= 3, margin = -0.5)
dev.off()
Update: I have tried square layout and the problem persists, here is my plotting object and square plot.
square plot
rda file for my igraph object
Can anyone give me an suggestion how to solve this issue? To whole net is about 170 vertices but I don't know why it cannot be displayed on output file well. I have tried different plot options in mai, mar but this seems to fail.
The reason you are getting this behavior is because you are specifying margin in your plot call. margin=-0.5 is telling R to extend the plot 0.5 units past the graphics device dimensions, below are three examples:
Your original plotting call, notice the clipping
pdf("withMargin.pdf")
par(mar=c(9,9,9,9))
plot(g, margin=-0.5)
dev.off()
Without the call to par, problem still presists but now youuse the entire dimension of the graphics device.
png("withoutPar_Margin.png")
#par(mar=c(9,9,9,9))
plot(g, margin=-0.5)
dev.off()
Lastly, removing the margin in plot
png("withoutplotMargin.png")
par(mar=c(9,9,9,9))
plot(g)
dev.off()
You're specifying a rectangular size for what looks like a square object. Try a square size, as in
pdf("graph.pdf")
This will use the defaults, which are square.
But, it's hard to know for sure since you haven't given us the object to troubleshoot for you.

R metafor forest-plots: xlim seems to be wrongly ignored

I am using forest.rma in the R package metafor. I want to use the xlim argument to specify the width of the plot. But this gets overridden when one of the x-values is outside that range. Here are two examples:
library(metafor)
par(mfrow=c(2,1))
rmaObject <- rma.uni(yi=c(1.3, -16), sei=c(0.8, 0.7))
forest(rmaObject, atransf=exp, xlim=c(-5,12), alim=c(-5,12))$xlim
forest(rmaObject, atransf=exp, xlim=c(-5,12))$xlim
In both cases I think the function should follow my instruction to use xlim=c(-5,12) and draw just a left arrowhead for the second bar, to show that the whole of the bar lies outside the left of the plot. But as the console output shows the first one sets xlim to c(-16,12) and the second one sets it to c(-20,12).
In other words forest forces xlim to encompass both bars completely, but I don't think it should do that if I specify xlim, and especially not if I specify alim as well. Is there an easy way for me to fix this?
The numbers in this example are real, though I have omitted most of my studies (i.e. values of yi and sei), which are mostly in the area of 0 or 1.
You can install the development version of metafor as described here:
http://www.metafor-project.org/doku.php/installation#development_version
It deals with this better. It still forces xlim to encompass the yi values, but no longer for alim. The summary polygon at the bottom still looks like crap though (I haven't built in any code to cut off the polygon shape at the alim values -- not as trivial as it may seem), but this may not be an issue once you add the rest of the studies.

color.plot.phylo in PICANTE, how do you change size (cex) of tips or pass commands to plot.phylo?

I found the color.plot.phylo in the package PICANTE which colors the tips of your tree based on a trait. It's a great function, but I haven't been able to pass other commands to the phylo.plot function, as suggested.
I'm especially interested in changing the size (cex) of the tips and node.label.
Also, is there a way to move the legend?
This function wasn't written in a way that makes it easy to modify those graphical parameters, because certain parameters such as the location of the legend and the size of the text are both hard-coded in the function. If you are somewhat proficient at R you could modify the code of the function to change some of those values. For example, modify the following line in the function:
plot.phylo(phylo, cex = 0.8, tip.color = tip.color, main = main, ...)
to eliminate the cex value and then you should be able to specify different cex values as an argument to the function. You can find help on writing/modifying functions in R elsewhere on this site and in the R documentation.

Plotting a box within filled.contour plots in R?

I'm trying to plot a box within a filled.contour plot, but unfortunately, when I plot the lines() after the filled.contour plot is created, the figure is shifted to the right because the scale forces the image to the left, but the box stays at the same coordinates. Here's what my code looks like:
dev.new(width=6,height=7)
mypredict<-matrix(data=mypredict,nrow=20,ncol=25)
filled.contour(x=seq(from=-1.5,to=1.5,length=20),
y=seq(from=1,to=3.75,length=25),
z=mypredict,
col=hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1)
)
top <- 3.42
bot <- 1.56
lines(c(-1,-1),c(bot,top))
lines(c(1,1),c(bot,top))
lines(c(-1,1),c(top,top))
lines(c(-1,1),c(bot,bot))
Does anyone know how I can plot those lines within the filled.contour function? Otherwise, the lines do not plot correctly onto the main image, since the scale/legend of the graph is placed on the right.
Thanks!
The manual page for filled.contour explains the problem (and gives a solution)
This function currently uses the ‘layout’ function and so is restricted
to a full page display. As an alternative consider the ‘levelplot’
and ‘contourplot’ functions from the ‘lattice’ package which work in
multipanel displays.
The output produced by ‘filled.contour’ is actually a combination
of two plots; one is the filled contour and one is the legend.
Two separate coordinate systems are set up for these two plots,
but they are only used internally - once the function has returned
these coordinate systems are lost. If you want to annotate the
main contour plot, for example to add points, you can specify
graphics commands in the ‘plot.axes’ argument. An example is
given below.
So essentially you pass some instructions as the plot.axes parameters to override standard behaviour.
In your example:
filled.contour(x = seq(from=-1.5,to=1.5,length=20),
y = seq(from=1,to=3.75,length=25), z = mypredict,
col = hsv(h=seq(from=2/3,to=0,length=20),s=1,v=1),
plot.axes = {axis(1); axis(2); rect(left, bottom, right, top);})
Note that you have to recreate the two axes otherwise they will not be drawn. Also, no need to use the lines statement, when there is a rect function! :)
Hope this helps

Plotting with R - skewed eps image & resolution problem

I want to create a graphics of a function in R. The code is:
x <- seq(from=0, to=1, by=0.00001)
f <- function(x) ....
y <- f(x)
plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5)
min(y)
[1] 0.2291203
max(y)
[1] 0.7708797
When I save the graphics as bmp from RGui, it looks like here and this is fine. When I save it as eps and include in LaTeX with:
\begin{figure}[htbp]
\centering
\includegraphics[scale=0.4]{./images/f-probart.eps}
\end{figure}
it is skewed, as shown in the screen capture from here
What is wrong? I guess there is a problem with exporting from RGui in eps, as the resulted eps is also shown skewed in IrfanView. Hence I suspect that is not the LaTeX inclusion code that is wrong...
How could I create this graphics with a requested resolution, say 244 dpi? Is there another package/function that allows me to export eps with a specific resolution?
Thanks
I cannot reproduce your error, so I guess it's something specific to your system. If I save as eps and include it in latex (using the graphicx package), everything works completely fine. Keep in mind that if you used the postscript() function in R, you have to specify the width and height of your picture as well. I could be wrong, but I think it defaults to the default values of the graphics window in R (which could explain the dimensions of your eps pictures).
If you saved from the graphics window, it normally should take the current width and height of the graphics window. It does so on my R version, but maybe your options are set differently? check ps.options() and see if width and height have value 0. If that's not the case, that could be the problem.
On a side-note : You could use pdf instead. See ?pdf in R. It allows you to specify the width and height of the picture, and reproduces correctly in Latex. You should use pdftex for building the file then.
My experience is that using pdf graphics and pdftex is less trouble than passing through PS. In fact, in latex there is no need to pass through eps any more to come to a decent pdf. Another advantage of using pdftex is that you can easily combine all graphics formats in the same document. (For EPS you need the epstopdf package)
2) the dpi requirement is only useful for grid images, so not for eps and pdf which are vectorized. I'd use png, that's the best format for graphs. See the option res in the function png().
png("somefile.png",res=244)
plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5)
dev.off()
Alternatively, you could use the function bmp() for bitmap graphics in exactly the same way. Don't forget the dev.off() at the end.
I used the Cairo package; the code was:
Cairo(24000,24000,file="a.ps",type="ps",bg="transparent",pointsize=12, units="px", dpi=2400)
plot(x, y, xlab="x", ylab="f(x)", pch=16, cex=0.5, type='l')
dev.off()
The resulted graph looked fine. One question, however: according to #Joris Meys, the dpi is useless for vector graphics; in this case, why specifying dpi for the Cairo function is mandatory?

Resources