Change the spacing in R dendrograms - r

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.

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.

R plotly height calculation for horizontal dot charts

I routinely use R plotly to draw Bill Cleveland-style horizontal dot charts. The category names are written in the left margin. I have widely varying numbers of categories and wish for charts with a small number of categories (down to only one category) to be as short as possible and not have vertical scrolling on the legend that appears in the right margin. I've experiment with a number of algorithms for computing the height needed in the plotly::plot_ly() call, and have it working pretty well when the number of categories is large. But not so well for a small number. Here is one attempt:
heightDotchart = function(rows, per=25, low=200, high=800)
min(high, max(low, per * rows))
I found that if I set the lower limit for height to 300 nothing is ever hidden, but the 1- or 2-category situation wastes too much vertical space.
I would especially like to compute height automatically so as to have the minimum height without omitting points on the plot and without having any labels colliding.
Ii understand that the plotly layout parameter autosize results in larger graphic areas than needed.

ggplot: Axis Text Label Width?

I'm currently struggling immensely to get the axis text in a graph that I'm designing to automatically format such that it cuts off when it becomes too long. I would ideally implement some sort of character cut off, but I'd ideally prefer to refrain from sticking '\n' after every 2 words. I've dug through the libraries for solutions, but couldn't find any.
It'd also be lovely if I could somehow prevent the graph from shifting upwards, as it makes it incredibly difficult to compare with the 'other' graph next to it.
I've largely been using ggplot and theme settings to make the changes.
Is it possible to make these changes? (1. width limit on axis text, 2. stop area expansion)

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")))

How do I increase the size of the points and the text with just one command in ggplot2?

I am plotting some graphs for a poster and a slideshow. I need bigger points and bigger text. I read about ggplot2's theme_set and theme_update. From what I can tell there are only two preset themes and they differ by the color arrangement of the background. However, I want to make all the text bigger and the plotted points bigger.
I learned how to change the font size.
theme_update(axis.text.x=theme_text(size=30))
But that only changes the axis text. I would have to do the same thing for a bunch of other parameters (axis.text.y, axis.title.x etc). Call me "lazy" but I want a single commands that can increase the base size for all text (and preferably the plotted points too). Is there one or two commands that covers all parameters? Alternatively are there any other set themes?
If you are fine with the colors of either of the two default themes, both take an argument of a base size for text. This is carried over to all the text around the plot (with scaling). You can just add theme_gray(30) to your plots. One caveat to that. If you afterward set other parameters of text with them_text, you have to respecify the size.
Alternatively, you can take the code for theme_gray (or theme_bw, whichever is closer) and make any thematic changes directly there. For examples of how to do that, check the ggplot2 wiki: https://github.com/hadley/ggplot2/wiki/Themes
EDIT:
As an example:
library("ggplot2")
qplot(1:2,1:2) + theme_bw(30)

Resources