I am unable to get graph from R - r

So I made a correlation graph on R. Now I want to save it as png on my laptop with a higher width, height and resolution but whenever I run the command and then do dev.off() a png file is saved to my working directory but its empty
I run this code it runs properly
png(file = "Corr.Fluconazole.HMF.png", width = 3000, height = 3000,
res=500)
dev.off()
on the console I get this
> jpeg(file = "Corr.Fluconazole.HMF.jpg", width = 3000, height = 3000,
res=500)
> dev.off()
RStudioGD
2
An empty file saves and I cannot see my graph

Related

How do I save SVG plots from Databricks in R?

I have the following R code which I can run in R Studio to generate a CDH dendogram plot and save it locally as a SVG.
c1 <- hclust(as.dist(subs_matrix), method = "ward.D2") #ward.D2
#scale the branches of the tree, only changes the aesthetics the dendrogram for interpretation
min_height<-min(c1$height) - 0.01 #Ensure tree has some height at its lowest point
max_height<-max(c1$height)
c1$height <- (c1$height-min_height)/(max_height-min_height)
#plot the tree, cex is the label font size and hang = -1 roots the branches
plot(c1,cex = 0.6, hang = -1, width=20, height = 8)
#Outputs the tree image to a file
dev.print(svg, file = "C:/Users/Users/c3523186/Downloads/plot.svg", width = 20, height = 8)
dev.off()
When running in Databricks however, I don't know if/how I'm able to save the svg, either locally or in the Filestore. The plot produced by Databricks is shown as an image, which is too low quality / not scalable.
Can anyone help? Some research suggests it might have something to do with display_HTML(), but I have no idea how.

R png() Function Adding Extra Stuff to Filenames and Ignoring Image Size Flags

I have an R script working and it is nearly done but there is one final problem. I am using the png(filename = " ", width = N, height = M, units =" ", ...) function in R but it is appending the width, height, and units flags to the png filenames. I can't find a way in the default help to fix this.
The exact command I am using is
for(i in 1:length(lst)){
png(paste0(filename = paste( lst[i]), ".png", width = 1280, height = 688, units = "px"))
...all plotting code...
dev.off()
}
Where lst[i] a list of filenames read through a loop. I end up with the .png files being called
idq27qecq.png1280688px where 1280688px is the stuff I dont want in the name. The png files are also all incorrectly sized, and have a size of 480 by 480 pixels.
How can I fix this?
If I remove the paste0 at the beginning I get the error
Error in png(filename = paste(lst[i]), ".png", width = 1280, height = 688, :
invalid 'pointsize' argument
and also
In png(filename = paste(lst[i]), ".png", width = 1280, height = 688, :
NAs introduced by coercion
Any help fixing the names and sizes of my output images would be helpful.
I looked at this post
save multiple plots r into separate jpg
but it did not work even though I tried to implement it.
All of this is being done in the Rstudio engine through a script.
The first paste0 is not needed, also the second paste should be used only to create filename value. Try :
for(i in 1:length(lst)){
png(filename = paste(lst[i], ".png"), width = 1280, height = 688, units = "px")
#
#...all plotting code...
#
dev.off()
}

Problem in magick package adding margings to an image and then annotating text

I want to read an image of 1500x1060px, add margins (aprox 3 cm at right) to it and in this margins plot a text. (It is in a shinny app that let the user change transparency).
If I do the text first and the margins after:
image1 = jpeg::readJPEG(file.path(paste(input$map1,'.jpg', sep='')))
image1 = abind::abind(image1, image1[,,1]) # add an alpha channel
image1[,,4] = input$trans1/100 # set alpha to semi-transparent
image1<- image_read(image1)
image1 <- image_annotate(image1, "Generated with my.webpage", location = "+1550+1000", degrees = 270, size = 40, color = "black", strokecolor = NULL, boxcolor = NULL)
png(outfile, width = 1500, height = 1060, units = 'px', res = 300)
par(mar=c(0,0,0,3))
plot.new()
rasterImage(image1, 0, 0, 1, 1)
dev.off()
The text is annotated in a zone that does not exist yet so it doese not appear.
So im trying to use the image_annotate after the par(mar=()) but I don't now how to do it, as image_annotate has to be used with an object as first variable.
As you presumably do not want to change the image to include rasterized text (poor quality), I would suggest to use the R buitlin function mtext to print the text on the margin, e.g.
img <- jpeg::readJPEG("image.jpeg")
plot.new()
rasterImage(img, rasterImage(img,0,0,1,1)
mtext("comment on image", side=1)
When opening a different device (pdf, png, or what else) before calling plot.new(), you can also save the annotated image to a file as shown in your example (do not forget to close the device with dev.off(), so that it is actually written).

Output Stem and Leaf Plot to Image

I'm trying to output a Stem and Leaf plot in R as an image. I'm not sure if there's a nice library which can accomplish this but below is some of the code I've tried.
jpeg(filename="stem.jpeg",width=480,height=480, units="px",pointsize=12)
plot.new()
tmp <- capture.output(stem(men, scale = 1, width = 40))
text( 0,1, paste(tmp, collapse='\n'), adj=c(0,1), family='mono' )
dev.off()
This above code resulted in the data being saved, but it looks very blurry and the plot gets cut off pretty badly. When adding a histogram to an image, R seems to do a good job to scale everything to fit in the size of the image.
jpeg(filename="stem.jpeg",width=480,height=480,
units="px",pointsize=12)
stem(men, scale = 1, width = 40)
dev.off()
This created the image but had no content within it.
Any ideas? Thanks!
That's because stem and leaf plots produce text not images. You can save the text as follows using the sink command: http://stat.ethz.ch/R-manual/R-devel/library/base/html/sink.html
sink(file=“Stem.txt”)
stem(men, scale = 1, width = 40)
sink(file=NULL)
unlink("stem.txt")
To export a stemplot as graphics, you can use a vector graphics format, such
as .eps, .pdf, or .emf. For example, a windows metafile:
win.metafile("stem.wmf", pointsize = 10)
plot.new()
tmp <- capture.output(stem(mtcars$mpg))
text(0,1,paste(tmp,collapse='\n'),family='mono',adj=c(0,1))
dev.off()

Copy R plot to clipboard with custom size

Is there a way to get R / RStudio to copy a plot to the clipboard with a custom size?
RStudio has this function, but you have to define the size everytime and there is some extra clicking which I am sure is avoidable.
I tried my best with saving as jpeg or else with file="clipboard" and then - after plotting - dev.off(). No error messages, but also nothing in the clipboard.
Here is an example:
data(mtcars)
jpeg(file = "clipboard",width = 800, height = 600, units = "px", pointsize = 12,
quality = 100,
bg = "white", res = NA, family = "", restoreConsole = T)
hist(mtcars$mpg)
dev.off()
Any ideas on how this can be achieved?
The best way would be to be able to control the size in Rstudio, but as you have found out yourself from the Rstudio-website, Rstudio doesn't support that. The following code saves your plot to wmf. There is also a workaround to a save to bitmap, which involves some clicking, but at least you don't have to specify the size any more:
data(mtcars)
windows(800, 600, pointsize = 12) #opens a separate window with the size you want
hist(mtcars$mpg) #draw to this (active) window
savePlot("clipboard", type="wmf") #saves plot to WMF
Unfortunately, it seems to be impossible to save to jpg format to the clipboard. You can copy it to a bitmap by going to this window, click CTRL-C and the graph is on the clipboard as bitmap with 800:600.
EDIT:
The windows command only works on Windows.
For Mac, it should be replaced by: quartz(width=8,height=6,pointsize=12,dpi=100) (width/height in inches!)
For linux try x11(width=8,height=6,pointsize=12,dpi=100) (untested).
I know this is an old post, but recently looking to do the same I came across this Gist which worked well:
https://gist.github.com/dpashouwer/4223903d3ed5783158b7e63992155649
library(tidyverse)
gg_to_clipboard <- function(plot = last_plot(), width = 1000, height = 600, pointsize = 40){
win.graph(width = width, height = height, pointsize = pointsize)
plot %>% print()
savePlot("clipboard", type = "wmf")
dev.off()
}
ggplot(data = mtcars, aes(x = mpg)) + geom_histogram()
gg_to_clipboard()
Making the function makes it very simple.
With Windows and RStudio, you click Export, click Copy Plot to Clipboard, and Copy Plot.
Then, paste into Word or PowerPoint or whatever.
No need to change sizes unless you want to.
This is not command line, but hardly seems onerous.

Resources