How to save plot in R - 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().

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()

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 download clear plot through rstudio?

I plot in rstudio, cannot see the number in plot, then I download as bigger size.
Whatever the png or jpeg format, even size is 2048 pixels, still cannot read the number in plot.
How to solve this problem?
RStudio has known sizing issue of saving pictures plotted in the "plots" window. What I would do is to save the picture directly in code.
Refer to this SO question
In short, what you can do is:
png(filename = pathToYourOutputPNG) # create a png device
plot() # write your plotting functions here
dev.off() # close the device

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.

How to resize pdf graphics produced in R using Illustrator

I am making plots in R using the pdf() command. Graphs look perfect and resize nicely in Acrobat Reader. My usual workflow includes manipulating labels etc. in Illustrator, saving as .eps for submission to publishers or inserting in Word. All works fine for single graphs.
Now I am trying to combine 4 graphs into one by manually putting them together in an A4 Illustrator document. However, when I resize the standard 7x7 inches pdf graph in Illustrator to fit in one column of an A4 page (ca 3.4 inches wide), all proportions get screwed up, e.g. lines and symbols outlines become way too thick. Using pdf(..., width=3.4, height=3.4) in R messes up all the symbol and font sizes so carefully chosen to produce the original graph. Why can't I resize the graph within Illustrator the same way I can resize the pdf e.g. in Acrobat Reader?
Illustrator is scaling everything including the stroke thickness, used to draw lines and symbols that have been converted to paths, and the font size for any text not converted to a path. (As I don't have Illustrator I can't say whether Illustrator treats "text" as text or as paths when opening the pdf.)
When Adobe Acrobat Reader displays the pdf it is just showing a rasterised view of the current file so just scales everything nicely as you wish.
I see two options; Either create the 2x2 plot directly in R and export that to PDF with the correct dimensions, or reduce the margins and font size used in each plot and export then at the desired width/height using the command you showed.
The first options can be achieved via:
pdf("attempt1.pdf", ....)
layout(matrix(1:4, ncol = 2, byrow = FALSE)) ## byrow = TRUE for fill-by-row
## all 4 plot calls go in here
layout(1)
dev.off()
You may need to tweak the point size used in the pdf() device and somewhat adjust the cex.??? settings for some bits of the plot to tailor this exactly how you want it.
Alternatively, you need to reduce the pointsize and margins and draw each plot on the 3.4 by 3.4 inch device. Something like this will get you started:
pdf("attempt2.pdf", height = 3.4, width = 3.4, pointsize = 10)
op <- par(mar = c(4,3,3,1) + 0.1) ## one line less per marging
## your single plotting call here
par(op)
dev.off()
See ?par for the list of ways to control the plot margins & other parameters you might wish to set to control the quality of the final plot. You may wish to look into the cex.foo parameters to control the relative sizing of the text on the plots, but this is all relative to the base pointsize you set when you create the pdf() device.

Resources