Prevent ggplot from auto-adjusting facet sizes in R - r

I have this problem where R will auto-adjust the size of the facets in ggplot. In the 2 attached images, clearly, the one scaled from 0-100 on the y-axis is less stretched out compared to the one scaled at 6.6-7.2. These are plotted using the same ggplot commands from maaply, so I don't know where the difference would come from. Is there any way to prevent R from performing the auto-adjusting to keep the formatting of each ggplot the same? My OCD and I thank you.

It looks like I have made a copy and paste error where I used some the the wrong variable to set the base_height in save_plot within mapply, so the scaling factor was varying across iterations.

Related

Resize plot canvas to actual plot size in R with ggplot2

One of the features of the R plotting machinery that I use more often is png(file=..., width=..., height=..., res=...).
Normally, I set precise values within the function, e.g. png("out.png", height=1500, width=2500, res=250).
I am now making a tool that makes an automatic plot from the provided dataset, but the tool is agnostic in respect to the number of rows it has to show in the plot, i.e. it plots what it receives. Sometimes, the plot has a large white area around it. Some other times, the canvas is too small and some rows fall outside.
I'm trying to fix this by calculating height and width according to the number of rows in the dataframe, but I find this approach error prone and suboptimal.
For example, inkscape has a nice function called "Resize page to drawing or selection" which will resize the canvas to match the boundaries of the plot. You can even pass a certain tolerance value so that your plot will still have some white around it.
Does R have this possibility, perhaps within ggsave() if not within png()?

strange break (white) lines presenting in the columns and rows of the heatmap in R

this problem confuses me for a long time
I use pheatmap to draw a 10000* 2000(more or less) size matrix , final plot will show some blank(white) lines inserting to the row and col of my main plot in most of the time, like some cut-off rules to my main plot.
I have checked my data , it has no NA rows or cols. And this cut-off lines will change position when adjusting the resolution of the plot. wheather ploting directly in Rstudio window or generating the plot file in work path, this problem can not be avoided either.
I guess this problem may due to the graphic system of R or the resolution or anti-aliasing settings, if someone can solve this, I would appreciate it greatfully.
Thank you.

How can I specify the exact number of pixels I would like my ggplot to take up?

How do I adjust the overall size of a ggplot?
I'm using Shiny, thus, I'd like to control the size of my plot. I'd like one of my plots to be the size of a postage stamp. And, I'd like the plot next to it to be huge.
From what I can tell, no matter how much I play with scale_x_continuous or xlim or cartesian_coord or whatnot, I'm still stuck with a ggplot that has made up it's own mind on how big it wants to be. I can squish down the amount of ink within the size of the plot, or I can fill up the insides of the plot by using various attributes, but I can't change the number of pixels the plot takes up on my screen.
How can I specify the exact number of pixels I would like my plot to be?
I don't know shiny, but maybe this helps,
library(grid)
print(qplot(1,1), vp=viewport(width=unit(114,"points"), height=unit(1.4,"inch")))

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.

Intelligent Y Axis Scaling BarPlot R

I want to plot some data with barplot. Rather, I want to make a bar graph and barplot seemed the logical choice. I am plotting just fine but I was wondering if there is a way to intelligently scale the y axis to round up from the highest count.
For example I set the yaxis in this case to be 30, because I knew that Strand.22 had 27 counts in it: barplot(unlist(d), ylim=c(0,30), xlab="Forward Reverse", ylab="Counts")
In the future, I want this script to run on its own, so it would be optimal for the the Y-axis to choose it's own ylim. Short of pulling the information out of my 'd' variable I can't think of a good way to do this. Is there an easy way to do this with barplot? Would some other plotter work better? I have seen things about ggplots but it seemed super complex and I wasn't sure that it would do anything better.
EDIT: If I do not choose a ylim it picks automatically and this is what it decided was best.
I disagree with it's choice.
If you don't specify ylim, R will come up with something based on the data. (Sounds like you don't like it's choice, which is fair.)
If you specify something based on the data like:
barplot(unlist(d), ylim=c(0,1.1*max(unlist(d)))
R will draw you a plot that reflects the maximum value of data. That example just takes the maximum of your values and multiplies that by 1.1 (this could be any number) to give it a little extra height. R does something similar to this when you make a scatterplot but it handles barplots slightly differently.

Resources