subplot into splitted screen in R - r

I would like to add a subplot to a plot in a splitted screen in R.
An intuitive and simple way would be:
par(mfrow=c(2,3))
plot(15:1)
par(new=T,fig=c(0.5,1,0.5,1))
plot(1:15)
I want the second plot (1:15) in the upperright corner of the first (15:1) and the same for five further plots. but unfortunately the fig-values are taken for the whole margin and not only for the first screen out of the 6.
what I also tried already:
split with
layout(matrix(1:6,2))
split.screen(6) # and fill the screens with a loop
As the real plots are quite complex, I also stored them with recordPlot and tried to fill the 6 part-screens with replayPlot. I also used subplot() (from package TeachingDemos) instead of the par(fig()) argument.
The result is the same all the time - the subplot is in the upperright of the whole margin.
Did anyone solve this problem or knows another way to try? There are few similar questions here, and none of them were answered... But it would be nice if this were possible ;-)
Thanks

Related

Prevent ggplot from auto-adjusting facet sizes in 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.

Displaying multipanel R figure

I have been working on a multi-panel figure in R. It displays a lot of plots/graphs... a number of rows and a number of columns. When I add titles to the ONLY the top row, they appear above the output rendering. Either it's too far above or it gets cut off.
The multipanel is pretty big, about six rows with three columns each. As every column represents the same thing, I figured to add titles above all of them. The titles at first were directly on the graph so I decided to raise them using the following:
title(main = "Title Example ", cex.main=1.7 , line = 1)
This elevates things nicely. But it goes way too high.
Previously I had issues with the size being too large. It was solved using one of the following two:
par(mar=c(1,1,1,1))
or
dev.new(width=16,height=10)
The titles just appear above the stuff. Any suggestions for what can be done to fix this?
I am hoping that the final output could just "lower" everything by a little bit.
But also, the output generation panel does not have a "scroll" sort of element. Would be nice if I can do that too... The monitor has only so much size, which does not fit the 6 x 3 fully.
It looks like it was fixed by using "par(mar=c(0.5, 7, 1.5, 0.5))"... this code increased the margin and so it worked.

Plotly Multi Column Horizontal Legend

I am trying to create horizontal bar chart in in R using the plotly package. Due to the length of the legend items I would like for them to show horizontally at the top or bottom of the visual in 2 columns. Is it possible to dictate the number of columns for the legend?
I've been able to place the legend below the x axis successfully using Layout(legend = list(orientation='h')) however regardless of where I put the legend (using the x and y arguments) it is always just one long list. I've seen a github project for creating a multi column legend in js but not r.
Thanks,
This is not possible in a normal way. I think it has its own logic that determines how many place there it is and how many columns it will display then.
So I guess if you make your plot width smaller you could reach the goal that it will just display 2 column.
Also you can try to play around with the margin attribute (https://plot.ly/r/reference/#layout-margin) by setting r and l to 10 e.g.
An other idea could be to make the font-size in legend (https://plot.ly/r/reference/#layout-legend-font-size) bigger, so that it just uses two columns. Hope it helps.
I read the same github page and I thought that it is not possible, but seems to be! I only checked in Python, but I hope this will help in your endeavors in R as well as everyone in Python looking for information. Sadly, there is not a lot of information on Plotly here compared to other packages.
This solved my problem
Setting orientation='h' is not enough. You also have to put the legend items in different legendgroups, if you want them in different columns. Here is an example with legend labels:
fig = go.Figure([
go.Scatter(x=best_neurons_df['Test Size'],
y=best_neurons_df['Training Accuracy Max'],
# You can write anything as the group name, as long as it's different.
legendgroup="group2",
name='Training',
mode='markers',
go.Scatter(x=best_neurons_df['Test Size'],
y=best_neurons_df['Validation Accuracy Max'],
# You can write anything as the group name, as long as it's different.
legendgroup="group1",
layout=dict(title='Best Model Dependency on Validation Split',
xaxis=dict(title='Validation Set proportion'),
yaxis=dict(title='Accuracy'),
margin=dict(b=100, t=100, l=0, r=0),
legend=dict(x=1, y=1.01,xanchor='right', yanchor='bottom',
title='',
orientation='h', # Remember this as well.
bordercolor='black',
borderwidth=1
))
Example image

Replace a plot under the par() function of R

There should be an easy way to deal with this, but I don't know. I'm plotting multiple figures with the par(mfrow=c(5,5)) subplot function of R (i.e. 25 figures). After plotting 10 figures say for example I've done something wrong with the 11th plot, now if I want to plot it again using plot function it takes the space for 12th subplot which means the whole subplot structure changes. I know that par(new=TRUE) would let me re-plotting on the top of the 11th figure, but what if the revised plot is so different that overlapping doesn't work? The idea is to erase the 11th figure and then plot it all over again. How about changing the 1st plot after plotting all 25 figures??
It is possible to use the screen family of functions, though I confess to not being an aficionado of them. As you would hope against, it is only to be used exclusive of par(mfrow=c(5.5)) or even layout(...).
Having said that, it is entirely possible to redraw over a screen. For instances:
split.screen(c(5,5))
for (scr in 1:25) {
screen(scr)
par(mar=rep(0,4)+0.1)
plot(0)
}
screen(7)
par(bg='white') # necessary for some display types
erase.screen()
plot(2)
(This is certainly not a beautiful example, but it is functional.)
Notice the explicit setting of the background color (bg) to white; with some displays where transparency is assumed, not doing this will appear to have no affect (that is, erase.screen() will do nothing).
Having said that, there are many modern and near-modern graphing functions/libraries/packages that do things that this package does not support. I have not tested this with image-capturing mechanisms (such as sandwiching things in png(file="...") and dev.off()). Caveat emptor!

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.

Resources