Adjusting size of plot in R - r

I have a distribution plot that I want like this:
dev.new(width=5,height=2)
plot(w.count.v, main="Distribution Plot of 'she' in Two Kinds",xlab="Novel Time",ylab=m=c(0,1),yaxt="n")
How do I save it as a .png as the size on the device? When I save it, this is the resulting image.

Related

Control how much space legend.only takes from a raster plot in r

I am trying to simply plot a raster map and a legend barr, but i cannot seem to get it at the right distance from the raster. Is either over it or too far. Also, despite i indicate a fixed width and height to the SVG printing function, the final size does not look similar. Would it be a way to control the exact position of the legend barr when plotting legend.only=T? By the way, stack overflow does not allow me to upload an SVG image, so i am uploading a screen capture that looks similar.
here is how i see it in R
Here is how i get it from the file generated
Here is the script used to generate that image, region and range are polygons, and hill and tmax are rasters. The plotting in R works fine-I just want to know how avoid the changes in the image from the R window to the file. There seems to be something in the exporting function that is creating a high white space between the map and the barr, and it express only when exporting the graph.
svg(paste("/Users/",spp[i],"filename", ".svg",sep=""),
bg="transparent", width = 5,height=5)
plot(region)
plot(hill,col=grey(0:100/100), add=T, legend=F)
plot(range,add=T, border="turquoise")
plot(Tmaxc>tmaxsp,col=heat.colors(100),add=T,legend=F)
plot(Tmaxc>tmaxsp,col=heat.colors(100),legend.only=TRUE, horizontal = TRUE,
legend.args = list(text='Warming tolerance\u00B0C', side = 1, line = 2))
dev.off()

How to save plot in R

I'm saving a plot in R that I made with ComplexHeatmap. However, when I save it as a PDF, I could see that there are horizontal lines in the annotation bar. I don't have this issue when I save it as a png file but the plot is blurry. How can I save my plot so that it is clear and the lines don't appear?
In R, the resolution of a plot saved as a PDF is related to the size of the PDF page. You can increase the size, and therefore the resolution, by adding to your code this:
pdf(width = 32, height = 18 , onefile = T)
Followed by the plotting code, and ending with dev.off().

How to increase height of image in R?

I am using heatmap package and plotting heatmap from 400 rows and 10 column so plot looks very dense. Is there any way to plot png or jpeg image (as need to include in website) where length is 20 times than width. I tried to use
png("heatmap.png",width=10,height=200)
heatmap(some_data);
dev.off();
But height of png image gets changed not the plot size. how can i change plot size?

R_controlling size of the plot in a multiple plot window

I am trying to include two plot in a single window using mfrowin par().
something like this
x=1:10
y=seq(10,100,10)
z=seq(100,1000,100)
par(mfrow=c(2,1))
par(mar=c(0.2,4.1,4.1,2.1))
plot(x,y,xaxt="n",xlab=NA)
par(mar=c(4.1,4.1,0.2,2.1))
plot(x,z)
which gives
Now let's say I want to adjust the height of second plot as small as half the height of the first plot. How can I do it? I was trying with many par() parameters. But I couldn't find a way.

How do I save plot images in R?

I have created a plot from a very large vector (magnitude of 10^7). The problem with the usual way of saving the plot as a pdf file is that the pdf file comes out as a very large file of around 10MB. I don't want such a large size for a simple time series plot. How do I save the plot such that the size is small enough to be at most 100kilobytes?
baptiste is on the right track with their suggestion of png for a nice raster type plot. In contrast to Jdbaba's suggestion of copying the open device, I suggest that you make a call to the png()device directly. This will save a lot of time in that you won't have to load the plot in a separate device window first, which can take a long time to load if the data set is large.
Example
#plotting of 1e+06 points
x <- rnorm(1000000)
y <- rnorm(1000000)
png("myplot.png", width=4, height=4, units="in", res=300)
par(mar=c(4,4,1,1))
plot(x,y,col=rgb(0,0,0,0.03), pch=".", cex=2)
dev.off() #only 129kb in size
see ?png for other settings of the png device.
If you want to plot the png file use the following command:
dev.copy(png,"myfile.png",width=8,height=6,units="in",res=100)
dev.off()
you can change res value to higher value if you want to output high quality graphs.
If you want to save the file as pdf use the following command:
pdf("myfile.pdf",width=8,height=6)
dev.off()
Remember to change the width and height values as needed.

Resources