Dealing with Large Legends in R Plots - Complicated Heatmap Example - r

I'm working on a really complicated heatmap figure in R. heatmap.2 from the gplots package is not enough for me, because I want multiple sidebar annotations, such as the heatmap.3 function permits: https://www.biostars.org/p/18211/
Here's my specific plot so far:
When I have multiple sidebar annotations on this heatmap, though, they need to be properly labeled with a legend. The legend quickly becomes unwieldy and starts bleeding off the plot or into other labels on the plot, depending on where I choose to place the legend.
I've tried using the ncols option when placing the legend at the bottom of the plot but the legend contains information about several factors worth of metadata, and I want a separate column in the legend to denote each sidebar's worth of metadata. As far as I know there is no option in the legend command to permit this functionality, so I'm interested in hearing potential ways around this.
Alternatively, I am also open to the idea of simply generating a legend image with R separately if anyone knows how to do this.
Thanks!

Related

plot panel visualization using ggplot2 in R shiny

I am implementing a R shiny with a plot panel implemented by library(ggplot2). If there are 12 plots, the layout looks great. Please check below.
12-plot layout
However, if I increase the plot number to 70, then each plot looks being compressed (pls see below). Is that possible I can keep the size of each plot fixed? Thank you so much!
enter image description here
Is there another way to approach this? For instance, can you group your data by two categorical variables and use on for colouring and the other for facetting? In that way, you may be able to reduce the number of facets, and stick with the larger facet size, while still conveying all relevant information? 70 facet plots is a lot!
Is this more of a QC thing? For QC, I tend to break it into groups by condition as Paul was suggesting. The reason is that within a condition, things should be really similar. Outside a condition, all bets are off. When I do this for genomics data, I tend to use “pairs” customized to my liking.
What don’t you like about the 70 sample display? Simply the change in aspect ratio? IMO, these are the things I don’t like about ggplot. You can make these plots using base R and then place them on a page manually using par or layout. For that matter, you can do the same with ggplot and use ggarrange or a different manual layout function to place the plots. All wrapped in a for or apply of course.
The other things I like to do when I have a LOT of QCs to look through is create a movie. I can use the forward/back buttons and go through a lot quickly. I like the idea of having this in a dashboard, nice one!
you could also try coord_fixed(ratio= ), not sure if that will work with faceting or not
Finally, I have made a movie-like visualization for those 70 plots using the plot_ly function in R package "plotly".

How to move the legend to outside the plotting area in Plots.jl (GR)?

I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)

Plotting in R multiple graphs without spaces

I’m aware that how to plot multiple graphs in a same window has been solved already and it’s pretty straight forward. But I can’t find how to remove the space between the different graphs.
I use this script to arrange these three graphs in the same window:
mat<-(matrix(1:3,ncol=3))
layout(mat,widths = rep.int(1, ncol(mat)),heights = rep.int(1,nrow(mat)),respect =F)
layout.show(n = 3)
With this script I can generate this graph:
These three violin plots where obtained from a large dataset. I don’t publish the script to get them since I think is not relevant for the purpose of this question.
As you can see this figure can be improved. First the labels at the x-axis are cut from the figure, I don’t know why this is happening. Also I want to remove the space between the three plots. Thanks!

Interactive plot in R (part scatterplot, part network)

I am trying to build an interactive plot. It has properties between a scatterplot and a network - I have a list of nodes and edges (network), but I also would like to constrain the nodes, sometimes on the x-axis sometimes on both x- and y- axis (scatterplot). Finally, I have a text label associated with each node that I would like to display (instead of a dot). I was able to create this using ggplot2.
However, some data sets are too large for this to work without the text labels from each node overlapping. Hence, I would now like to add an interactive feature so that the plot consists of dots representing each node, but that upon UI (such as hovering over a dot), the text label belonging to that dot will be revealed.
I would like to achieve this using R.
I tried animint (https://github.com/tdhock/animint) but it seems to mostly allow interaction between two plots, and here I would like to keep it all in one plot.
I also tried htmlwidgets (http://www.htmlwidgets.org/). I looked at two of their packages: I tried using metricsgraphics (mjs_plot), as it has a show_rollover_text option and mouseover option. However, this package does not allow combination of geoms, and so I could not have both dots (nodes) and lines (edges) represented. I also tried network3D package, but that seems to automatically position nodes so that they are distanced far away from each other, and does not seem to provide options to fix each node on a given x and y location.
I am just looking for advice on any other packages I should maybe consider to solve this problem and/or if I may be missing a feature from a package I already tried that could solve this problem. Thank you.
Maybe identify() will be useful for you. But it works only for base plotting system.
x <- rnorm(300)
y <- rnorm(300)
labs <- seq(300)
plot(x,y)
identify(x,y, labels = labs, plot=TRUE)
identify pic

varying stat_binhex() size in ggplot2

I'm trying to use the stat_binhex() in ggplot2 to drop hex tiles on a plot, and the automatic settings vary the color of the bins, depending on count. That is, all the hexes are the same size, but have different colors.
I want to vary the size of the hex symbol itself! so that some are bigger than others... and i also want to vary color based on a third variable. I read through the documentation of ggplot2 and couldn't find any way to do this. The *hexbin* package has an option like this (lattice) but its plot() functions are maddening, so I was hoping to stay in ggplot2. Any other suggestions would be extremely helpful, as well.
If you know Kirk Goldsberry's NBA shot charts on Grantland, that's very similar to what I'd like to accomplish with my dataset.

Resources