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)
Related
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.
I am plotting polygon in R and saving it.Problem, I am facing is that the whole plot is saved as png file but I want to save only the filled area in the polygon.
Is there a way for that ?
x<-c(0.000000000,0.010986328,0.006351471,-0.004634857)
y<-c(0.000000000,0.007232612,0.012841203,0.006199415)
file_name = paste("~/Downloads/Plot", ".png", sep="")
png(file_name,width=1280,height = 720,units="px",res=200)
plot(x,y,axes=FALSE,ylab='',xlab='')+polygon(x,y,col="#FF0000FF")
dev.off()
If you're drawing a monofigure plot (which is the default), then I believe there are three possible sources of spacing that can cause a plot element to not extend to the edges of the graphics device:
1: data coordinate limits that are larger than the extent of the plot element.
2: "internal spacing", which is best thought of as an expansion of the plot area that sits inside the margins.
3: margins. This is normally where axes, ticks, tick labels, axis labels, titles, and sometimes legends are drawn.
All of these sources of spacing can be eliminated with the following customizations:
1: set the xlim and ylim graphics parameters to perfectly fit the target plot element.
2: set xaxs='i',yaxs='i', which can be done with either a preemptive par() call or on the initial plot() call.
3: zero the margins with mar=c(0,0,0,0). This must be done with par() prior to the initial plot() call.
Example:
## generate data
pts <- data.frame(x=c(0.2,0.4,0.9,0.7),y=c(0.5,0.4,0.5,0.6));
## precompute plot parameters
xlim <- range(pts$x);
ylim <- range(pts$y);
## draw plot
par(mar=c(0,0,0,0));
plot(NA,xlim=xlim,ylim=ylim,xaxs='i',yaxs='i',axes=F,ann=F);
points(pts$x,pts$y,pch=21L);
polygon(pts$x,pts$y,col='red',pch=21L);
Multifigure plots can incur one additional source of spacing, namely outer margins, but it looks like that doesn't concern you for this problem. In any case, I'm pretty sure outer margins always default to zero anyway.
See par() for the relevant documentation.
It looks like I misunderstood the question. What you want is a transparent background, which is different from simply fitting the image size to the plot element.
You can use the png() function to set the background to be transparent by passing bg='transparent', as explained on the documentation page.
For example, here's my fitted image saved with a transparent background:
Note that not all image viewers will correctly detect and/or clearly depict the transparency of the background. I would highly recommend GIMP, which is basically a free Photoshop knockoff, albeit markedly lighter in features. GIMP depicts transparent regions as a kind of checkerboard of grey squares, which looks like this:
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
Okay I have been working with gnuplot for sometime and have one question. I cannot make the plot size equal to the terminal size I set it to. There are examples of setting
Lmargin,rmargin, tmargin and bmargin to 0 but this doesn't work on 'splot' which is 3d plotting. So I want to know what is the workaround for it?
For color maps you can use plot ... with image instead of set pm3d map if you want to use the margin options as usual. For 3d plots, as you say, the margin options are not available, and a workaround would be to shift the position of your graph and scale it. To do that, use set origin and set size respectively.
For instance, splot x*y yields the following:
If you want to reduce the margins while keeping the same overall terminal size, you can try:
set origin -0.1,-0.1
set size 1.2,1.2
splot x*y
which gives you:
You probably get the idea. Note I set a background color just so you can visualize where the margins lie because of the white background in the Stack Overflow website.
I recently tried to reproduce the Crime in Downtown Houston, Texas made by David Kahle, in order to reproduce it for another analysis later on.
Everything works well, and I can generate map as I want... However, there is an issue I cannot explain.
When I try to save the file at a larger format (let's say, 4500x1546 instead of the normal 1266x435), the size of the points, axis, title, legend etc. do not automatically adjust : they become way too small.
To illustrate this:
First image= 1200x435 (normal format), second image= 4500x1546 (adjusted format),
I observe the same effect with a geom_density plot..
I guess the solution is really trivial, but I didn't find it...
If you want to create a higher resolution png there are two approaches:
Increase the size in e.g. inches of the plot
Increase the dpi, the number of pixels per inch
You chose the second solution, this makes all the elements smaller, which is expected behavior. If you want to increase the resolution without changing the elements, increase the dpi. This can easily be done using ggsave:
g = ggplot(...)
# low res png
ggsave("lowres.png", width = 6, height = 6, dpi = 50)
# high res png
ggsave("hires.png", width = 6, height = 6, dpi = 600)
In this way there is no need to work with base_size.