Displaying multipanel R figure - r

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.

Related

How can I adjust the position of tip labels relative to tree tips on a phylogenetic tree?

I am trying to create a figure of a map of a continuous trait onto a phylogenetic tree using the ape and phytools packages in R for a publication. An example code of what I am trying to produce is as follows:
library("ape")
library("phytools")
orig_tree<-rtree(n=350)
plot(orig_tree)
values<-data.frame("residuals"=runif(350,min=-1,max=1),row.names=orig_tree$tip.label)
values<-setNames(values$residuals,rownames(values))
residualsignalfit<-fastAnc(orig_tree,values,vars=TRUE,CI=TRUE)
obj<-contMap(orig_tree,values,plot=FALSE)
plot(obj,type="fan",fsize=.1,lwd=0.5)
The only difference is the terminal of the branches are all the same length because they are all living taxa, but this works well enough to illustrate the problem I am having. I have a large number of taxa in this tree, and as a result I have to shrink the text down fairly small using the fsize= argument to make them legible. However, as you can see from the example code, doing so causes the ends of each species name to be obscured by the outline for the phylogenetic tree. I have tried removing the outline but it makes the heatmap of the phylogeny very hard to read. I have been unable to find any way to reduce the thickness of the outline, it seems to be automatically generated.
I also tried adding the cex command to plot(obj...), but it has no effect on the produced tree at all.
What I am trying to figure out how to do is how to position the tip labels in order to make them more legible and not covered up by the outline for the tree. I cannot simply add a space in front of each terminal using the dplyr mutate function or something like that because the position of the taxon name is not always consistent, sometimes the left side of the name is attached to the tip and other times it is the right side. I have tried not plotting the data as a fan, but this ends up creating a figure with a huge amount of dead space due to the fact I have some very deep splits within the tree (basically plotting the figure as a right-facing tree results in half of the figure being dead space because my taxa diverged in the Mesozoic but only speciated after the K-T boundary).
Instead of shrinking the text down, you can scale the tree up. You can export the plot as image with a specific width and height. Setting both to 20 should make all tip labels legible.
Something like
setEPS()
postscript("output.eps", width = 20, height = 20)
plot(obj,type="fan",lwd=0.5)
dev.off()
This saves the plot to output.eps instead of showing it in the default viewer. So it is not restricted by the screen size. You still need to fiddle around with the best values of lwd and fsize, but in my experience it is much easier, when you have a big canvas.
Edit: Sorry, the unit of width and height is inches not pixels. So rather set it to 10 or 20.

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

Change the spacing in R dendrograms

I want to create a nice dendrogram for my cluster. Since I work with over thousands of data entries, the resulting dendrogram looks kinda sad. The Screenshots show, that too much space is used to display the splits, while not enough space is left to show the important information at the bottom.
I want to compress the space used for for the graph display and increase the space used for the labels. Is there an option for this? If I only decrease the the size of the image, he will simply cut the labels at the end.

subplot into splitted screen in 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

facet_grid with many factors becomes unreadable / increase size of plot?

Apologies if this is a very trivial question, but I have spent a few hours already on this. I have a ggplot with a facet grid which includes several factors. The problem is that the headings of the facets are not readable. There are 'too many'.
facet_grid(years.since.peace ~ type_of_termination+strategy.cm6.YP)
I know that I could change the font size of the panel headings, or split it into several different graphs.
But what I am interest in is to enlarge the plot area so that there is enough space for all headings of the panels/facets? The graph is likely to end up as a poster, so printing it in e.g. A2 would be fine.
I checked the space, scale and shrink options - but it seems they are not meant for what I am trying to do. I also tried to modify the pdf() command with its width and height options. But the graph simply becomes larger without solving the problem of the unreadable facet headings.
Maybe I am simply wrong with what I am trying to do?
you could try:
library(ggplot2)
ggsave('testplot.png', height = 11, width = 8.5)

Resources