Skip to content

legend position #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
blueskypie opened this issue Jan 24, 2021 · 3 comments
Closed

legend position #669

blueskypie opened this issue Jan 24, 2021 · 3 comments

Comments

@blueskypie
Copy link

is there a way to put the legend at any coordinate in the figure?

Thanks,

@jokergoo
Copy link
Owner

Legend are grob objects. You can construct a single legend by Legend() and arrange multiple legends by packLegend(). Later the legend can be put at any place in the plot by draw(lgd, x, y, ...).

See https://jokergoo.github.io/ComplexHeatmap-reference/book/legends.html

Note if you do this way, the space for the legends is not calculated anymore, which means, you need to manually arrange the white space for legends.

m = matrix(rnorm(100), 10)
Heatmap(m)

lgd = Legend(title = "foo", at = letters[1:4], legend_gp = gpar(fill = 1:4))

draw(lgd, x = unit(0.5, "npc"), y = unit(0.5, "npc"))

image

@blueskypie
Copy link
Author

How to get the automatically generated legend objects by Heatmap() and HeatmapAnnotation()? so they can be included in packLegend() along with those legend object specifically defined by Legend().

Here is my code, where lgd1 and lgd2 are two objects defined by Legend(), named as WK24.PC and cytokines.
Capture
TD2TI and Hgb.RESP are two auto-generated legends for row annotation. log10 BL/mean is the auto-generated legend for the heatmap body.

I'd like to move all the legends to a coordinate. But if I add x = unit(0.5, "npc"), y = unit(0.5, "npc"), it doesn't work for draw,Heatmap-method

cRe=Heatmap(...)
draw(cRe, annotation_legend_list = list(lgd1,lgd2), merge_legend = TRUE)

@jokergoo
Copy link
Owner

Then you need to manually construct all legends. It will be something like:

lgd = packLegend(
    Legend(title = "TD2TI", at = c("FALSE", "TRUE"), legend_gp = gpar(fill = c("red", "greem"))),
   ... # same for other discrete legend,
    Legend(title = "log10\nBL/mean", col_fun = your_col_fun, ...),
   ... # other legends
)

and remember to turn off all the default legends:

Heatmap(..., top_annotation = HeatmapAnnotation(..., show_legend = FALSE), show_heatmap_legend = FALSE)
draw(lgd, x, y, ...)

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

No branches or pull requests

2 participants