Show a square plot under chunks in Rstudio - r

I am programming in Rstudio using markdown files, alternating between Markdown and R chunks of code. If I plot something in a chunk, the plot is shown below the chunk.
Does anybody know how I can make the plot look square instead of rectangular?

use this kind or header of chunk with fig.width = fig.height
<<R_chunk_title,echo=FALSE,warning=FALSE,message=FALSE,fig.width=6,fig.height=6,cache=TRUE>>=

Related

Decide exact position of figures and plot in knitr R Markdown

I had trouble position exactly my plots and figures when knitting to pdf a R Markdown document with knitr.
This question helped me: https://community.rstudio.com/t/cant-control-position-of-tables-and-figures-in-knitted-pdf-document/37364 , where the answerer uses a text file to fit in the latex instructions for the knit.

Low quality figures using RMD to create latex docs

Good morning,
I have decided to try RMarkdown to create short white papers that I will be updating regularly. The rmd code extracts Fed data, organizes it, and then creates graphs that are placed alongside some short commentary about them.
My question is about the output. The figures created within Rstudio are crisp and what I wanted; the pdf output should the lines as thicker and much less crisp.
My code chunk is below. I've tried changing the dpi at the top of the chunk, but that has not changed the pdf output.
Any ideas about getting the same crisp lines within RStudio onto pdf using Rmarkdown?
Thanks!
```{r echo=FALSE,dpi=600,message=FALSE}
# Create caption
mycaption<- "Source: FredII - Federal Reserve Bank of St. Louis"
# Wrap caption 120 characters:
mycaption <- paste0(strwrap(mycaption, 120), sep="", collapse="\n")
# Create Plot
ggplot(data=dt2,aes(x=date,y=value,color=name,linetype=name))+
geom_line(size=0.7)+
labs(x="",y="Interest Rates",
title="Comparing Interest Rates by Source/Maturity",
caption=mycaption ) +
guides(title="New Legend Title")
```
An easy solution to have higher quality plotting output when compiling to PDF is to use the tikz graphics device by setting the chunkk option dev="tikz" (or globally using opts_chunk$set(dev = "tikz")).
Tikz is a TeX package that allows to create vector graphics using an easy syntax. Using vector graphics has the advantage that they are scalable, annotations in your plot will use the same main font that is used in the document and you can easily add math symbols to your plot using the TeX syntax:
plot(..., main = "$\\bar{\\mu}$")

Saving AND showing plots in rmd file

I'm working on a rather long code using R markdown, divided into chunks. Plot appear under the appropriate chunk. I'd like to keep this behaviour, but additionally I want to save them to a specified folder. I've tried different methods listed here How to save a plot as image on the disk? (and elsewhere on the Internet), but nothing seems to work.
My reproducible example:
png('cars_plot.png')
plot(cars)
dev.off()
This code saves the plot, but doesn't show it (it only returns "null device 1"). I've also tried dev.print and dev.copy, with the same result.
Thank you in advance!
Clarification: I run my chunks one by one, I don't want to convert my results to pdf/html yet. So knitr: include figures in report *and* output figures to separate files or change where rmarkdown saves images generated by r code don't answer my question.
You can always graph it twice in the same markdown chunk, like this:
plot(cars)
png('cars_plot.png')
plot(cars)
dev.off()

Control size of points in figure produced by knitr

I've used knitr to produce a PDF. This is a screenshot from 1 page of the PDF:
After a couple of hours of fiddling various knitr options (fig.width, fig.height, out.height, out.width), the page of the PDF looks half decent. But...the axis text, the annotations and the points in the plot are still too small relative to the text in the PDF.
Is there is a way to have knitr automatically size the text/points in the plot so fit well inside the PDF?
EDIT: with the suggested fig.width="3.7" and out.width = "\\columnwidth"

knitr makes multiple figures instead of adding just lines() to axes

I have a very simple Rnw file:
\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\begin{document}
<<mychunk, echo=FALSE, cache=FALSE, fig.keep='all'>>=
plot(rnorm(100),rnorm(100))
lines(c(-1,1),c(-1,1))
#
\end{document}
The R chunk
plot(rnorm(100),rnorm(100))
lines(c(-1,1),c(-1,1))
should just give one figure. Instead, the output tex is
\includegraphics[width=\maxwidth]{figure/mychunk1}
\includegraphics[width=\maxwidth]{figure/mychunk2}
with two separate figures
and
This doesn't happen in all instances of a plot and lines, depending on the arguments, but the code I've given reproduces the problem. It should just have the second figure.
You've set fig.keep="all", and are getting just what would be expected from that setting! As described in the the online documentation, that option "keep[s] all plots (low-level plot changes may produce new plots)".
Try instead fig.keep="high" (the default, so you can also get it by just leaving out the fig.keep argument) or fig.keep="last", depending on what exactly you want.

Resources