R program from Wikipedia doesn't plot - r

I want to use a R script which is publicly available from Wikipedia: https://commons.wikimedia.org/wiki/File:Correlation_examples2.svg .
This program is supposed to output an image but for some reason when I paste the code to Rstudio it doesn't plot.
What can I do to make it output the image?

Two main reasons:
The script by itself doesn't plot anything. It just sets up the function that will generate the image. So pasting the code by itself won't help and you need to call the 'output' function.
However the function by itself doesn't plot anything either. It saves the plot as Rplots.svg which contains the .svg code to plot the images.
To display the plot in RStudio, use for example the magick package.
output()
magick::image_read_svg('Rplots.svg')
Alternatively you can open the Rplots.svg file with a browser of your choice.

Related

How to using code to save plots in Rstudio?

Is there any way to save the plots by code in RStudio?
Like something in Python save.plt().
I have noticed the function savePlot can save the figures but I cannot load it in RStudio, since it reports the errors in loading X11().
This leads me to think about another question... What is your way to using R or what is the proper way to using R? As a beginner I found RStudio is super easy to use and I like the idea that saving all the environments into a single file. But apparently it is no need to using X11() in plotting...
If you want to save from code the plot that is displayed in the RStudio plot tab then you have to call for example
rstudioapi::savePlotAsImage("test.png",width=300,height=150)
Plots can be saved as a jpg:
jpeg("Name_of_your_plot.jpg")
# your plot for example
plot(x,y)
dev.off()
Instead of jpg you can choose other image formats such as png, pdf or PostScript. The above code can be modified as:
png("Name_of_your_plot.png")
# your plot for example
plot(x,y)
dev.off()
If you have a ggplot you can use ggsave.

Is there a way to specify portrait/landscape orientation per page using pdf()?

I am trying to find a way to set the rotation per page. For example, this is the trimmed down code I am using:
pdf('test.pdf',
paper='USr', width=90, height=12, onefile=TRUE)
grid.draw(grobtable1)
grid.draw(ggplot1)
grid.newpage()
grid.draw(grobtable2)
grid.draw(ggplot2)
grid.newpage()
grid.draw(table.portrait)
dev.off()
I would like all the pages except for the last one to be portrait. Is there any elegant way to do this using pdf()?
I have no experience with R Markdown yet, as I have read that might be a way to do what I desire. However, the code that I gave is something I use as a function, of which the plots and tables are being called from another function output. Hence, I thought using R Markdown would require me to make new Markdown files all the time.
I'm not sure if you can alter the pdf orientation in the middle of a run. You could write out to two files and merge them together using the staplr package. See here.

How to save graphs/plots from quatrz generated by bio3d (R package)

I'm new to R and bio3d and have been unable to find any answers to my problem. I'm trying to find a way to save graphs/plots generated by bio3d (a R package). So far I have to manually click save as when the graph appears and I have tried many variations of R language to save the graph which either result in no saved file or a small file that cannot be opened. Can anyone give me some pointers please?
In an R script you may try with the following lines:
pdf('nameoftheplot.pdf', width=..., height=...)
Then you can write the R-code that generates your plot, and at the end you should add this last line:
dev.off()
Select all the lines and run them with cmd+R (Windows) or cmd+enter (OS X). The output pdf file with the plot should be located in your current working directory. Hope this works.
Edit: if you want a .png file as an output you have to replace the first line with:
png('nameoftheplot.pdf', width=..., height=..., res=...)
Edit2: Example:
pdf("firstplot.pdf", width=6, height=3)
qplot(carat, data = diamonds, geom = "density")
dev.off()
If you have already created your plot (i.e. it is displayed in the active quartz or x11 window) you can use dev.copy2pdf() and it relatives, e.g.
plot(c(1:10))
dev.copy2pdf(file="example.pdf")
If you want to do this without plotting to the quartz/x11 window then issue a call to png() or pdf() etc before your plot() call and then follow it with a dev.off() call, e.g.
pdf(file="example2.pdf")
plot(c(1:10))
dev.off()
The 'plot.new figure margins too large' error can occur when your plot window is too small to take all the graphic output you are trying to produce. Often making your window bigger will solve this.

R loop through complete script, find all plots generated and save them

I have the following problem using R and not found a solution so far:
I have a script where I run several operation and generate some plots. At the end, I would like to have a nice piece of code that automatically saves all the plots generated into the current working directory. So far, I am using:
trellis.device(device="png", filename="Plot_A.png")
print(Plot_A)
dev.off()
Which is working fine for just one specific plot. Now I am looking for some kind of for loop that takes all the plots and saves them with the name of the plot as a png file
In grid based plotting packages (lattice and ggplot), you can store the plot in an object and call print on them to trigger actual rendering of the plot. What you could do is not render the image on the spot, but append any plots to a list. Then, at the end, you can loop over the plots and output them.
plot_list = list()
lattice_plot = xyplot()
plot_list = append(plot_list, lattice_plot)
for(plot in plot_list) {
png('name.png')
print(plot)
dev.off()
}
Not exactly an answer but an alternative workflow.
If you are saving your plots in order to use it somewhere else, for example to include them in a Word document or in a presentation, you could just put your code in an RMarkdown document and knitr it to generate an html or doc document with all the output generated by the code, including plots. With RStudio all that could be done with a few clicks.
It may even be easier to take all plots from the Word document than from a folder of png files.

Easy way to copy paste base graph from R Studio into word document

I need to copy paste R graphs into MS Word.
rando<-rnorm(1:100)
plot(rando)
when I copy the .png graph from R Studio into Word I get a negative space version of the graph:
Is there a cleaner/easier way to do this? I would be happy to use pdf or something else to present the graph.
As #Roland suggested:
Export -> Copy Plot to Clipboard (window with plot will pop-out) -> Metafile -> Copy Plot -> Paste to MSWord.
This seems a lot of clicks to me, rather as #user2633645 suggested save all plots as png then insert them in MSWord in one go.
?png
rando<-rnorm(1:100)
png(filename = "rando.png")
plot(rando)
dev.off()
You can use the devEMF package. It is repeatable, easy to use, and converts very nicely to pdf if needed.
(wow is this an old question, but I guess it's still an issue. I'm on RStudio v1.3.1073)
I came across 3 options:
Cross posting from https://github.com/rstudio/rstudio/issues/5103#issuecomment-679021780: "I've found two simple workarounds: Paste Special and choose TIFF, or (my preference) drag the image from the zoom window and drop in PowerPoint"
Snipping tool
You can specify the dimensions like in the RStudio plot export
windows(800,600) ## Opens graphic window. ctrl c/v works here.
plot(rando)
If you are doing this copy-plot-to-Word often or for several plots, consider creating an .Rmd file with your code, call knitr on that file, and use system("pandoc to convert your knitted .md file to Word.

Resources