tableGrob not fully shown between two plots using grid.arrange in R - r

My specific issue is evident when I output 3 plots to a html report using grid.arrange, sometimes the last plot (which is a tableGrob) is cropped.
I am using 3 separate grid.arrange() statements to place a boxplot, a time series plot and a tableGrob in a html report using knitr.
Code being used to output is:
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(over5)>0){
tg=tableGrob(over5,theme=tt3,rows = NULL)
tg$widths = unit(rep(1/4, ncol(tg)), "npc")
grid.arrange(tg)
}
This displays perfectly when the tg object has less than roughly 10 rows of data.
However in testing larger datasets I found that the grid.arrange(tg) outputs in a fixed height "window" in the report which is not desirable.
My question is how can I change the height of the grid.arrange(tg) ouput to match the height of the tg object contained inside (with some top and bottom margin)??
I would appreciate understanding the mechanism by which these properties are changed so I can fine tune my output. I would have a 3x1 grid display all 3 outputs in one line of code if possible but all plots get condensed and unreadable so that is why I have 3 separate grid.arrange() statements. I Guess my real issue is I don't understand how there properties are manipulated here.
Help is greatly appreciated.

I found a workaround to use kable which prints a HTML table to the report instead of grid.arrange() with tableGrob() :
grid.arrange(p1,top=main)
grid.arrange(p2TS)
if(nrow(tg)>0){
print(knitr::kable(tg))
}
Also as I was running this code within a for loop I needed to use a workaround on this issue page which requires me to wrap the kable in a print() statement.

Related

How do I size plots correctly in Rmarkdown?

When I create a plot from a normal R script, click Export and Copy to Clipboard... I get a nice sized plot which works well to paste in a presentation. The size in the Copy Plot to Clipboard plot shows Width of 1065 and Height of 652. See below -
However when I create the sample plot in an rmarkdown and knit, I get something like this -
How can I play with plot settings in rmarkdown to produce an output like the first screenshot?
Hi there so you should stop using the console interface totally for this. You want more control over the size of plots than that can offer you. Also doing it the way you do it offers no means to autogenerate plots, what happens when you need to run a loop and make 100 images or even just like 10 or 20? That's a lot of unnecessary manual labor
Here's how you can do this....
dev.off() # this clears the plotting function
jpeg("filename", width = 100, hieght = "100") # note you can use png or
# other commands, this creates an image file for you to call a plot function on
plot(data) # any plotting function may be called here, ggplots pheatmap etc..
dev.off() # this causes R to save the image of the plot to file
Controlling image size in Rmarkdown, there is documentation on how to do this which is located here: https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html
I recommend doing what I showed above, supplying the code in markdown but not necessarily running it every time you knit the file. Instead its better to make the plot you want once then load it into markdown from a saved location so you can fiddle with the size easier. It will save you a ton of time running the same computation unnecessarily multiple time each time you knit. Again supply the code you used to make the plot in the Rmarkdown for completeness sake but don't actually run it. Making an Rmarkdown file will require you to knit multiple times repeatedly as you fiddle with the settings on your images and the text itself. Best to avoid running computation as much as possible, especially if your plots take a while to generate (heatmaps for instance can be a little heavy).

Knitr renders graphic although it shouldnt

I am using RStudio: Version 1.0.136, and I try to understand why knitr renders the histograms entailed in the commands below. Any help is appreciated.
min_ct<-as.numeric(min(hist(myfdata[myfdata$slope>low & myfdata$slope<up, ]$dy, breaks = bi)$counts))
Screenshot of 4 rendered graphics, which are not explicitly generated.
It's not a knitr issue. Calling hist causes a histogram to be rendered even if you assign the output to a variable. In the console try x = hist(rnorm(100)). What gets saved to the variable is a list with the data used to generate the histogram, but the histogram is still printed.
To create bins without printing a histogram, use the cut function to create the bins, then use table to count number of values by bin. For example, table(cut(rnorm(100), breaks=seq(-3,3,0.5))).
cut has options that affect how it assigns bins, so take a look at the help (?cut) for more info. In particular, take note of the right and include.lowest arguments.

scaling the R exported plot to match R's output plot

This may sound weird, or impossible but I can say the scale of the output plot when you compile inside R and view it without zooming or exporting works great for me. However, when I export to pdf, the plot becomes outrageously large and I never manage to scale it to what I see inside R. Am I being silly, or there is actually a difference and there is a way to get what I see inside that bottom-left-corner box of the R.
use ggsaves width and length arguments to get the desired size.
ggsave('name.pdf', width =14, height = 8)

Change plot size of pairs plot in R

I have this pairs plot
I want to make this plot bigger, but I don't know how.
I've tried
window.options(width = 800, height = 800)
But nothing changes.
Why?
That thing's huge. I would send it to a pdf.
> pdf(file = "yourPlots.pdf")
> plot(...) # your plot
> dev.off() # important!
Also, there is an answer to the window sizing issue in this post.
If your goal is to explore the pairwise relationships between your variables, you could consider using the shiny interface from the pairsD3 R package, which provides a way to interact with (potentially large) scatter plot matrices by selecting a few variables at a time.
An example with the iris data set:
install.packages("pairsD3")
require("pairsD3")
shinypairs(iris)
More reference here
I had the same problem with the pairs() function. Unfortunately, I couldn't find a direct answer to your question.
However, something that could help you is to plot a selected number of variables only. For this, you can either subset the default plot. Refer to this answer I received on a different question.
Alternatively, you can use the pairs2 function which I came across through this post.
To make the plot bigger, write it to a file. I found that a PDF file works well for this. If you use "?pdf", you will see that it comes with height and width options. For something this big, I suggest 6000 (pixels) for both the height and width. For example:
pdf("pairs.pdf", height=6000, width=6000)
pairs(my_data, cex=0.05)
dev.off()
The "cex=0.05" is to handle a second issue here: The points in the array of scatter plots are way too big. This will make them small enough to show the arrangements in the embedded scatter plots.
The labels not fitting into the diagonal boxes is resolved by the increased plot size. It could also be handled by changing the font size.

layout inside layout in R

I'm using R to create a heatmap from a matrix using heatmap.2 - and i want to group these images into one big image - What i usually use to achieve this is layout() - but this doesn't work, as heatmap.2 uses layout, and apparently layout does not work recursively.
Does anyone have any suggestions on how to group together 2 images without layout, or how to make layout support recursive calls?
mat = matrix(nrow=3,nrow=3,1:9)
layout(matrix(nrow=2,ncol=1))
heatmap.2(mat) ## overrides the layout and produces only one plot that takes whole screen
heatmap.2(mat) ## still only one image
thanks.
What follows is a hack that is almost certainly not a perfect solution, but it may get you started.
Create your own version of the heatmap.2 function called hm3. In the code for hm3, comment out all the lines between:
if (missing(lhei) || is.null(lhei))
and the layout call:
layout(lmat, widths = lwid, heights = lhei, respect = FALSE)
it's a big chunk of code, maybe 30 lines. Now the following code produces two heat maps with dendrograms and keys side by side:
x <- as.matrix(mtcars)
lhei <- c(1.5, 4,1.5,4)
lwid <- c(1.5, 4,1.5,4)
layout(rbind(c(4,3,8,7),c(2,1,6,5)),
widths = lwid, heights = lhei, respect = FALSE)
hm3(x)
hm3(x)
Clearly, this will require considerable tweaks to make it look nice (and a larger plotting area; I've squished everything to be a reasonable size to post here).
This is entirely untested. It is likely that using any of the options in the hm3 function that control the appearance of the plot will cause things to go slightly haywire. But this may be a good starting point for your own experimentation to address those issues.
What are you planning on doing with the results?
If you just want to compare 2 heatmaps side by side on the screen then rather than combine them into one single plot you can open 2 plotting devices and arrange them side by side to compare (much simpler than creating a single graph):
heatmap.2(mat1)
dev.new()
heatmap.2(mat2)
Now drag one to the side of the other using your mouse.
If you want to include the combined graphic in a publication then it may be easiest to create the 2 plots and just set them side by side in whatever program you are using to create the article. If you need them in one file you can still save the 2 heatmaps (or other plots) as 2 files then use tools such as imagemagick, gimp, or inkscape to combine the 2 files into 1 with the graphs side by side.

Resources