I am using the following code to export some tables as pictures. Using the code below can export the table but there is a lot of white space around it which I want to remove. Also is there a way to autosize the images as tables have varying rows?
png("picture.png", width = 650, height = 600, bg = "transparent")
grid.table(df)
dev.off()
Expected output will remove the white space that comes around the table and the actual picture should fit the size of the table only
Related
Below is a screenshot from quarto document with a wordplot I've generated using wordcloud package in R.
The challenge is that there is a lot of white space between the words on the plot and the border of the plot image.
How do I reduce the size of the extra 'white space'?
Reducing the white space might be way more complex than these two options! Hope that can help as well.
You can change the size of the letters with the wordcloud2 package and fill in the white space.
Example: wordcloud2(data=demoFreq, size=1.6)
You can save the image without the white space and upload it manually in Quarto.
Example: ![Image1](file_name.png)
I'm trying to add multiple plots from ggplot into my Latex report. Most plots have different dimensions, as some need to be rectangle shaped, where as some need to be square shaped. To shape the plots, I've been changing the height and width parameters in ggsave. However, this also changes the font sizes. How do I have consistent font sizes for all my plots, and also be able to change the width and height of each plot separately?
I've already specified the font size in the plot using theme(text = element_text(size=5)), but it doesn't seem to actually stay consistent between the plots.
Just like in title: I'd like to add several external images to my .docx document. But when using body_add_img I need to specify width and height. Is there a way to set them to width and height of original image to be added?
Why I need that? My images (about 50 of them) have all different widths and heights, so it would be painful to manually put their widths and heights in (about 50) body_add_img calls.
If your image is a png you can png::readPNG to get the width and height in pixels, and divide by your DPI to get the dimensions in inches. (Replace 300 with your DPI)
dpi <- 300
img_size <- dim(png::readPNG('image/path/here.png'))/dpi
Edit:
If you want the dpi in the doc to be the same as the dpi in the image natively (assuming your png has the dpi stored, I think not all do), use dpi <- attr(readPNG('image.png', info=T), 'info')$dpi
Long story short, I make youtube videos for my students and I produce a lot of graphics. Up until now, I have imported the images into premiere, resized them, then placed a separate white background image behind the ggplot graphics. This is a bit tedious and I would love to be able to export directly from R into the appropriate size (1920x1080 p) so I don't have to resize and add a background image.
I was able to resize the graphic, but now it stretches it:
I want it to look like this (I put the box around the image and the canvas to make it clearer where the plot ends and the background canvas begins):
I know I can specify the outside margins (see How can I control the canvas size in ggplot?), but that would require me setting the outside margins per plot; Sometimes I have plots that are wider than they are tall and sometimes I have plots that are taller than they are wide. ggsave seems to respect the aspect ratio and maximizes one dimension or the other.
So how do I set the outside margins while maintaining the correct aspect ratio?
You could set the panel size to a specific value,
library(egg)
library(ggplot2)
library(grid)
p <- ggplot() + labs(x = 'x title', y = 'y title')
ggsave('notset.png', p, width = 6.4, height = 3.6, units = 'in', dpi = 300)
ggsave('set.png', egg::set_panel_size(p, width=unit(4, "in"), height=unit(3, "in")),
width = 6.4, height = 3.6, units = 'in', dpi = 300)
I have this plot. I am using the following package and script to do a biplot. But the figure output always has white margins on the top and bottom of the actual figure. I tried removing them using plot.margin=unit(c(0,0,0, 0), but no luck. Any thoughts on how to do this or is it just the default and there is nothing that can be done?
The script (I am using the example, but the same issue with mine):
>library(devtools)
>library(ggord)
>ord <- prcomp(iris[, 1:4])
>p <- ggord(ord, iris$Species)
>p
The problem is the white margins on the top and bottom as seen here. How can I remove them?
The white space you are seeing is a function of the window size when you save the image. If you resize your window vertically or horizontally, you'll notice that the white space shrinks or grows.
If your goal is to save the plot as a PNG image, you can export it directly to file with your desired dimensions as follows:
png("plot1.png", 800, 300);
p;
dev.off();
Adjust the 800 to the desired length and the 300 to the desired height, in pixels. If the size is larger than the actual graph, it will pad with blank space.