This question already has answers here:
Plot size and resolution with R markdown, knitr, pandoc, beamer
(2 answers)
Closed 8 years ago.
I'm starting working with R Markdown and I'm not understanding how to generate bigger plots. With today screens the plots can be much bigger.
How do I control the plots width/heigh in a Markdown report?
Thanks for the help.
See this SO answer for how to specify these details in chunks.
To set global chunk options, use opts_chunk$set(out.width='750px', dpi=200) inside your first chunk. See this page on chunk options for more.
Related
I have a plain R script, not written using R Markdown. In it I create several graphs using ggplot. I then knit the script via File > Knit Document.
I have two problems with the output. First, the plots are not as wide as the rest of the output. Second, the resolution of the plots is not very good.
Is there any way I can increase the width and resolutions of the graphs without having to rewrite the script using R Markdown?
You can use the knitr spin syntax to provide chunk options for your plots. This should increase their width (and probably also fix the resolution issue):
#+ fig.width = 3
plot(1) # narrow plot
#+ fig.width = 9
plot(1) # wide plot
This question already has answers here:
Reduce PDF file size of plots by filtering hidden objects
(3 answers)
Closed 6 years ago.
I have saved several pdf plots with large file size in R, the problem is that I will need to import them into Latex, which takes a lot of time. I am wondering how to save a plot with smaller file size in R? Thanks.
Example 1.
seasonplot(ts(hdemand$Demand,frequency=24),
col=rainbow(length(hdemand$Demand)/48))
dev.print(device=pdf,file="hourdemand.pdf")
dev.off()
Example 2. (Even the fitted plot takes times because of the size of data)
par(mfrow=c(1,2))
plot(data$Temp,all.data$Demand)
abline(lm(data$Demand~data$Temp), col="red")
plot(data$APX,data$Demand)
abline(lm(data$Demand~data$APX), col="red")
dev.print("LR.pdf",device=pdf)
dev.off()
Reduce the number of points you are plotting.
But if you can't do that, and don't care about how it would look when zoomed in, write out as png at a convenient dpi and then convert to pdf outside R.
This question already has answers here:
Making flattened pdfs in Sweave
(3 answers)
Closed 9 years ago.
I am trying to include a single, less than one-page-sized plot into a Sweave/R pdf document. This plot is based on huge amounts of data - i.e. in a small plot area there are tens of thousands of points. Whenever I include the plot normally through Sweave, I get huge lag when I open the resulting pdf. This is similar to the case of exporting an eps with tens of thousands of dots - even though the plot area is small it will lag heavily.
How do I code it such that a png is inserted, or equivalent, which doesn't keep all the information of every dot in the plot but just keeps the information of the pixels corresponding to the plot size?
\begin{figure}
\begin{center}
<<fig=TRUE,echo=FALSE,height=4>>=
plot(rnorm(100000))
#
\end{center}
\caption{Visualisation in Sweave which can lag computers}
\end{figure}
I am looking for a LaTeX solution. This means no PNG
Use png like:
\begin{figure}
\begin{center}
<<label, fig=FALSE>>=
png('label.png')
plot(rnorm(100000))
dev.off()
#
\end{center}
\includegraphics{label}
\caption{Visualisation in Sweave which can lag computers}
\end{figure}
Or use the Sweave driver from here.
An alternative (not a direct answer to the question asked) is to replace a scatterplot with large numbers of points with a hexagonal binning plot instead. The hexbin package (bioconductor) or the ggplot2 package both have functions for creating the hexagonal binning plots. These plots will be smaller/faster than a scatterplot that contains many points, and for that many points the hexbin plot may even be more meaningful.
This question already has an answer here:
Call to plot doesn't actually produce plot
(1 answer)
Closed 9 years ago.
I am generating a plot which I am able to see in the RMarkdown output but whenever I am trying to save it, I am getting just a blank(white) image. I am just adding following two lines before and after plot
png("Output.png")
#Plot code
dev.off()
It was working. And suddenly it stopped working. Can someone help me on this??
edit
When I do it with pdf
pdf("output.pdf")
#plot code
dev.off()
I am getting error as:
There was error opening this document. This document cannot be opened because it has no pages.
Thanks.
From the comments I gather that I was right, you need to print the resulting ggplot2 object in order to show the plot, see also R FAQ 7.22.
This question already has answers here:
Creating a Movie from a Series of Plots in R [closed]
(7 answers)
Closed 5 years ago.
Does anyone have any tips regarding making animated GIFs in R? I'm trying to make some time lapse map GIFs.
Repeat Question : Please see this previous StackOverflow question on creating a series of plots in R which offers a number of solutions including pointers to other useful packages such as the animation package.
Also be sure to check out the animation package
I'd recommend just generating a series of png files (which happens by default each time you run plot with the png device open). You can then combine them into an animated gif with another program such as ImageMagick.
Here is sample code of using Imagemagick (called through R) for any sequence of png's you generate.