Skip to content

improvements on plotting functions that use 'groupby' - #1210

Merged
fidelram merged 96 commits into
masterfrom
groupby_plots
Jun 29, 2020
Merged

fidelram merged 96 commits into
masterfrom
groupby_plots

Conversation

@fidelram

@fidelram fidelram commented May 12, 2020 •

Copy link
Copy Markdown
Collaborator

This PR addresses issues from #979 and #1103 related to sc.pl.dotplot and replaces PR #1127

Furthermore, the current PR attempts to unify common code between dotplot, matrixplot and stacked_violin plots while at the same time adding more flexibility to the plots.

This PR also makes possible to plot fold changes, log fold changes and p-values from sc.tl.rank_genes_groups as suggested in #562

To achieve this the sc.pl.dotplot, sc.pl.matrixplot and sc.pl.stacked_violin method had been transformed into wrappers for the new DotPlot, MatrixPlot and StackedVioling classes. Accessing the new classes directly allows further fine tuning of the plots.

The use of the sc.pl.dotplot, sc.pl.matrixplot and sc.pl.stacked_violin didn't change, only the new parameter return_fig was added.

For clarity, the relevant code has been moved to scanpy.plotting._groupby_plots.py.

The new plot classes are all descendants of a BasePlot class that captures most of the common code shared between the plots. The design of the classes follows the method chaining (as found in Pandas or Altair). This allows the addition of independent features (via well documented methods) to the plot without increasing the number parameters of a single function. This was first suggested here #956.

For example, for dotplot is now is possible to add category totals, define the titles for the colorbar and size legend and modify the values for edge color and edge line with for dot plot and the size exponent used (among may other options). For example:

adata = sc.datasets.pbmc68k_reduced()
markers = {'T-cell': ['CD3D', 'CD3E', 'IL32'], 'B-cell': ['CD79A', 'CD79B', 'MS4A1'], 
           'myeloid': ['CST3', 'LYZ']}
dp = sc.pl.dotplot(adata, markers, groupby='bulk_labels', return_fig=True)
dp.add_totals(size=1.2)\
    .legend(color_title='log(UMI count+1)', width=1.6, show_size_legend=True)\
    .style(cmap='Blues', dot_edge_color='black', dot_edge_lw=1, size_exponent=1.5)\
    .show()

All objects have consistent functions for legend, to set up titles and width, style() to set visual parameters specific to each plot like colormap, edge color, linewidth. swap_axes to transpose the figure, add_dendrogram with options to change the with of the dendrogran and add_total tho show a bar plot of the total number of cells per category. Also includes options to sort the categories.

image

Here is description of changes:
all figures:

  • Set a title to the image.
  • Pass an axe where to plot the image.
  • Return a dictionary of axes for further manipulation
  • using return_fig the plot object can be used to adjust the proportions to the legend and other visual aspects can be fine tuned.
  • a bar plot with the totals per category can be added. This will align at the left of the image if the categories are plotted in rows or at the top if they are plotted in columns.
  • legend can be removed
  • groupby can be a list of categories.

dotplot

  • Improved the colorbar and size legend for dotplots. Now the colorbar and size have titles, which can be modified using the colorbar_title and size_title arguments. They also align at the bottom of the image and do not shrink if the dotplot image is smaller.
  • Plot genes in rows and categories in columns (swap_axes).
  • Using the DotPlot object the dot_edge_color and line width can be set up, a grid added as well as several other features
  • sc.pl.rank_genes_groups_dotplot can now plot pvals and log fold changes

matrixplot

  • added title for colorbar and positioned as in dotplot
  • sc.pl.rank_genes_groups_matrixplot can now plot pvals and log fold changes

stacked_violin

  • [update] violin colors can be colored based on average gene expression as in dotplots
  • made the linewidth of the violin plots smaller.
  • removed the tics for the y axis as they tend to overlap with each other. Using the style method they can be visualized.

TODO:

  • Update tests
  • Update tutorial and readthedocs

…or'. Relative sizes had been adapted to work in most conditions
…dot plot. Added options to plot pvalues and logfoldchanges
…ws and columns. Fixed order of rows and columns to match the ouput of matrixplot.
…956). Now is possible to do sc.pl.dotplot(adata, ...).add_dendrogram(size=1).show()
…ly the indices were kept. Useful to sort categories by name
…gories by pandas index sorting (iloc -> loc).
…max, color_map etc). Added `color` parameter to `.add_totals()`.
@fidelram

Copy link
Copy Markdown
Collaborator Author

@gokceneraslan I just added the dynamic coloring of the circles when the color is on the background square.

adata = sc.datasets.pbmc68k_reduced()
markers = {'T-cell': ['CD3D', 'CD3E', 'IL32'], 
           'B-cell': ['CD79A', 'CD79B', 'MS4A1'],
           'myeloid': ['CST3', 'LYZ']}

sc.pl.dotplot(adata, markers, groupby='bulk_labels', return_fig=True)\
             .style(color_on='square', cmap='binary').show()

image

@gokceneraslan

Copy link
Copy Markdown
Contributor

Looks phenomenal! Thanks so much.

@fidelram fidelram removed the WIP label Jun 26, 2020
@fidelram
fidelram merged commit b910afc into master Jun 29, 2020
@ivirshup

Copy link
Copy Markdown
Member

@fidelram Sorry, I completely missed your asking for a review! It does look good though.

However, it looks like this is breaking the docs. Could you take a look into that?

@ivirshup

ivirshup commented Jun 30, 2020 •

Copy link
Copy Markdown
Member

I think the issue that there are references to the DotPlot class, but no doc page get's generated for that class. I think this can be fixed by adding something like:

Classes used for these plots:

.. autosummary::
   :toctree: .

   pl._dotplot.DotPlot

to the doc-string of the plotting module.

Once this exists, there might be some other errors that pop up.

fidelram added a commit to fidelram/scanpy-tutorials that referenced this pull request Jun 30, 2020
This was referenced Jul 7, 2020
falexwolf pushed a commit to scverse/scanpy-tutorials that referenced this pull request Jul 14, 2020
* updated notebook after scverse/scanpy#661

* updated visualization tutorial after scverse/scanpy#1210  Also rewrote some parts to make them more clear

* updated notebook after @falexwolf review

* moved notebook to plotting folder
@fidelram
fidelram deleted the groupby_plots branch July 22, 2020 14:41
@gokceneraslan

Copy link
Copy Markdown
Contributor

@gokceneraslan I just added the dynamic coloring of the circles when the color is on the background square.

@fidelram The cool dynamic line coloring feature seems broken now, I wonder why :/

image

@fidelram

Copy link
Copy Markdown
Collaborator Author

Is because we changed dot edge the defaults shortly before the release. Time to add a test for this.

I will make a fix but meanwhile you can trigger the dynamic coloring by setting dot_edge_color and dot_edge_lw as None:

sc.pl.dotplot(adata, markers, groupby='bulk_labels', return_fig=True)\
             .style(color_on='square',  dot_edge_color=None, dot_edge_lw=None).show()

@flying-sheep

Copy link
Copy Markdown
Member

I wonder why the default for size_exponent is 1.5 …

Unless there’s a bias in human perception that suggests this, I’d say only a value of 2.0 makes sense (meaning area corresponds to value)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants