Change the size of the image itself in R - r

I use "plot" function in R to draw a picture.I want to change the size of the image itself instead of the drawing area,then I will save it to show elsewhere.
I have tried to use the following code.
par(cex = 2) # change the size of the content in the image not itself
plot(c(1,2,3),c(1,2,3),type = "p")
But it can only change the size of the content in the image not itself.I hope to get your help.

the documentation for the par() function in the graphics package lists all the parameters you can set for the plot.
The parameter you want is "pin" (plot dimensions (width, height) in inches)
so then
par(pin = c(5,3))
sets plot width to five inches, height to 3 inches.
The parameter "cex" is defined as a numerical value giving the amount by which plotting text and symbols should be magnified relative to the default.

Related

Can't save phylogenetic tree to pdf with color

I am trying to save my phylogenetic tree with coloured branches using discrete data, everytime I do the pdf() function and then try to open it I get a pdf error.
This is the code I am using
tree = read.nexus("phylo.1966")
tree3<-read.csv("BBS1966.2015.pgls.csv",row.names = 1)
tree3<-setNames(tree3[,1],rownames(Nesting))
cols<-setNames(palette()[1:6],levels(Nesting))
Nesttree<-make.simmap(drop.tip(tree2, setdiff(tree2$tip.label,names(Nesting))),Nesting, model = "SYM"
plotTree(Nesttree,cols,type="fan",fsize=0.8,lwd=3,ftype="i")
add.simmap.ledgend(colors=cols,x=0.9*par()$usr[1],y=0.9*par()$usr[4],prompt=FALSE,fsize=0.9)
pdf("Nesting1.pdf", width = 100, height = 100)
dev.off()
however, if I place the pdf function before the plotTree function it will open but no color is added to the tree.
tree2 is from a code used previously which just took my original tree and added the subspecies to make a new tree.
Also if I try to manually save the plot as a jpeg the size of the tree remains the same but the plot area will change to the size I chose, so it looks small and super crowded.
Thank you for any help.
This is the image I produce after using the corrected code
As I commented you can manually adjust the width and height of images if you images is crowded. You can use any device in place of jpeg (e.g. PDF) for pdf however you don't need to set height or width.
jpeg("filename.jpeg", height = 1080, width =1080)
plotTree(Nesttree,cols,type="fan",fsize=0.8,lwd=3,ftype="i")
add.simmap.legend(colors=cols,x=0.9*par()$usr[1],y=0.9*par()$usr[4],prompt=FALSE,fsize=0.9)
dev.off()

How to save an image in R exactly as it is 'stored'?

I generated a random example plot with ggplot2 and multiple facets. I am interested in the exact positions of the individual panels and want to export the relative start and end coordinates of those panels as well as the picture of the plot (basically for point identification via mouseover events in a javascript context).
To get the information i need for the relative coordinates i use the grid package. Suppose p to be the variable which stores my plot (after p <- ggplot(...)).
With
pb <- ggplot_build(p)
pta <- ggplot_gtable(pb)
heights <- pta$heights
and analogously for the widths I get that information.
For example, in this case I get for heigths
[1] 1lines 0cm+0lines 1null
[4] 0.25lines 1null 0.25lines
[7] 1null 0.532222222222222cm 1grobheight+0.5lines
[10] 0.5lines
where the entries with unit null represent the panels. To be able to compare these I have to convert them into the same unit. null I cannot convert but the remaining ones. Using that the viewport is 1npc (npc is a unit too) in width and length I can solve an equation which allows me to create the following image (screenshot from the RStudio Plots window) with the lines drawn with grid.lines():
So everything seems to work. Now if I rescale the Plot window in RStudio the lines are drawn at the wrong places. The reason is that some units like grobheight are dependent on the current viewport. Hence, if I want to save the image (say 600 x 600 pixel) i have to do
png(fn, width = 600, height = 600)
pushViewport(viewport(width=0.5, height=0.5, xscale=c(0, 600), yscale=c(0, 600)))
print(p)
... #draw the lines
dev.off()
But.
Despite that, no matter how I save the image, say ggsave() or png() (the latter either with print(p) oder grid.draw(pta)) the actual proportions differ from the computed ones which were correct in the R environment.
A certain amount of the plotmargins seems to get cut off, and additional scaling is done apparently.
A png export looks like this for example:
The difference seems to be small, but that doesn't help. So, is there a way to save the image exactly as it is described in the gtable object pta? Is it a matter of dpi or pointsize?
Many thanks for your help,
Mika
Everything I've encountered with this exact issue stems back to the height and width call in the png() or pdf() or anything output call. Play with the height and width in order to best determine what it will look like.
If you want to know what it will look like when you export use x11(height= width= ), note the units here are "in" so you will want to mimic that in your output function.

Scilab: same legend size in multiple subplots

I want to have a multiplot in scilab with a separate legend in each subplot. What I get so far is this:
I get that the Legend entity is a child of the axis entity of the figure entity. What I don't know is how to adjust the size so that all plots and all legends are the same width. I only see the entries for position etc. if I go for figure.children(1).children(1). How do I access the legend size? Thanks!
I don't know either how to change directly the size of the legend: it seems to me, that it is always automatically calculated based on the length of the text. But there is a solution to get equally sized plots: you can set the size of the plot by the margins property of the axes. The second number adjusts the size of the "empty" space on the right hand side:
x=1:0.1:6; //generate some data
y=[sin(x);sin(2*x)];
scf(0); clf(0);
subplot(2,1,1);
plot2d(x',y');
a=gca(); //get the current axes
a.margins=[0.05,0.2,0.125,0.125]; //set the margins
legend("y1","y2",-1,%T);
subplot(2,1,2);
plot2d(x',y');
a=gca(); //get the current axes
a.margins=[0.05,0.2,0.125,0.125]; //set the same margins to get equally sized plots
legend("y___1","y____2",-1,%T);
The help says:
A vector [margin_left,margin_right,margin_top,margin_bottom] specifying the margins portion for this axes. This vector is composed of numbers between [0 1] with default: [0.125 0.125 0.125 0.125]. These numbers are ratios relative to associated values of the axes_bounds property, which are width for margin_left and margin_right, and height for margin_top and margin_bottom.

change the font size and margin of R graph

I am experimenting with the investigate_var_importance in package of bartMachine
investigate_var_importance(bart_machine_cv, num_replicates_for_avg = 20)
It turns out that the generated graph is so big, especially the text label along with the x-axis. The default [R Graphics: Device 2(Active) cannot even hold the whole picture. How to change the font size and margin of this plot?
margin can be set by par(mar=c(bottomMargin, left, up, right))
see par to set that parameter
c(0,0,0,0) will not leave any space. You might not want this, as there wouldn't be any space for the axis.
par(oma=c(bottomMargin, left, up, right))
sets the outer margins of the plot
if you send the plot to a pdf, you can increase the size of the plot, which will be able to hold the whole plot i.e,
pdf('nameOfplot.pdf', 20, 6) ## opens a device, and produces a file much wider than longer
plot whater you want
dev.off() ## closes the device you have opened with pdf
if you play with those parameters you might be able to fit your plot

ggplot2 grid.export plot size SVG

How can I specify the width and height of the final SVG when I do:
print(plot)
saveXML(grid.export()$svg)
I'm producing plots for a web application in SVG. I was using ggplot2 and saving the plot using ggsave which allowed me to specify size in inches for width and height. I worked great. I now needed to perform annotations on the SVG and it seemed that ggsave did not work, so I'm forced to use the grid.export approach presented above. The only aspect I seem to have control is the resolution, which allows me to make plots smaller. However, while ggsave made all the text and point size scale to a bigger size, the grid.export approach changing the resolution can make the plot smaller, but just by makes everything smaller so the plot reads worse.
For instance, setting width and height to 3
ggsave version:
grid.export version:
What I managed to do is define a new base_size on the theme to compensate the resolution scaling:
base.size = 10 * (7/size)
resolution = 72 * (size/7)
if (length(plot$theme) == 0) plot <- plot + theme_gdocs();
plot$theme$text$size = base.size
mysvg <- grid.export(res=resolution)

Resources