R plotly height calculation for horizontal dot charts - r

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.

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()?

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.

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.

Create flexible ggplot2 theme that 1) makes the legend and titles larger, 2) will look good irrespective of the final dimensions

I currently am one of the few R users in my company, which consists predominantly of stata users. One problem I've had with making plots using ggplot2 is that the default (theme_grey()) settings have much smaller axis font and a smaller legend than what is found in stata. Moreover, in presentations I find people have trouble reading the legend and titles from a distance.
I'm aware that ggplot2 has a theming system that allows for relative sizing. What I'd ideally like to do is to create a new default theme that I'd apply to all my plots that would make legends and axis titles larger. Importantly, however, very often the graphs I make have varying dimensions when output to pdf (e.g. 8 inch x 10 inch) or ( 10 inch x 13 inch). Since I'd like to apply this theme globally, I need it to produce good/easy to read output irrespective of the dimensions I specify when outputting to pdf.
I'd really appreciate any suggestions for how to do this/how to approach the problem using ggplot2's theming system.
Thanks!
The theme system can easily scale all the (themed) text, but not in a device sized aware way. The various theme sets, including theme_bw(), have an argument base_size which is the baseline size, in points, of fonts to use for the text. Some text is rendered at that size, and some is rendered at sizes relative to that (for example, axis labels and legend labels are rendered at 80% of the baseline size). So by specifying the base_size argument, you can scale all that text.
However, the base_size is in absolute points, not in a size which is relative to the device size. So the larger the device size, the smaller the text is relative to that.

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