YuLab-SMU / aplot

Decorate a plot with associated information
https://yulab-smu.top/aplot
97 stars 14 forks source link

Adjusting Legend Position with insert_ Functions #6

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hi,

I am trying to adjust legend position in one of your example. For p2 graph (heatmap like graph). I am interested in to have legend at the bottom of this graph but instead legend for p2 is always coming to the right of the last inserted graph. Is it possible to change guides position after joining graphs ("What I need" on attached picture)?

Thanks, Dawid

library(ggplot2)
library(ggtree)

set.seed(2019-10-31)
tr <- rtree(10)

d1 <- data.frame(
  # only some labels match
  label = c(tr$tip.label[sample(5, 5)], "A"),
  value = sample(1:6, 6))

d2 <- data.frame(
  label = rep(tr$tip.label, 5),
  category = rep(LETTERS[1:5], each=10),
  value = rnorm(50, 0, 3)) 

# tree
g <- ggtree(tr) + geom_tiplab(align=TRUE)

# p1 = bar_graph
p1 <- ggplot(d1, aes(label, value)) + 
  geom_col(aes(fill=label)) + 
  geom_text(aes(label=label, y= value+.1)) +
  coord_flip() + 
  theme_tree2() + 
  theme(legend.position='none')

# heatmap like graph
p2 <- ggplot(d2, aes(x=category, y=label)) + 
  geom_tile(aes(fill=value)) + scale_fill_viridis_c() +
  theme(legend.position="bottom", legend.box = "horizontal") # adding guide positions

# draw final
p2 %>% insert_left(g) %>% insert_right(p1, width=.5) 
Screen Shot 2020-11-21 at 6 04 05 PM
jmonroynieto commented 3 years ago

I have the same issue. Even when the individual plots have legend.position = "bottom", the legend shows up on the side.

My current workaround is to export as SVG and modify it on Inkscape.

GuangchuangYu commented 3 years ago

in v>=0.1.0.991 (current github version),

pg =p2 %>% insert_left(g) %>% insert_right(p1, width=.5) 

print(pg) & theme(legend.position='bottom')

image

wenbostar commented 3 years ago

Is that possible to keep legend setting for an individual subplot?

GuangchuangYu commented 3 years ago
 options("aplot_guides" = "keep")
p1 <- p1 + theme(legend.position="top") 
p2 %>% insert_left(g) %>% insert_right(p1, width=.5) 

image

wenbostar commented 3 years ago

Tried to update aplot to use the new function but got an error as shown below:

* preparing ‘aplot’:
* checking DESCRIPTION meta-information ... OK
* checking for LF line-endings in source and make files and shell scripts
* checking for empty or unneeded directories
* building ‘aplot_0.1.0.994.tar.gz’
* installing *source* package ‘aplot’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
Error: object ‘ggbreak2ggplot’ is not exported by 'namespace:ggfun'
Execution halted
GuangchuangYu commented 3 years ago

need to install yulab-smu/ggfun first.

wenbostar commented 3 years ago

It works. But it looks like the legend function still doesn't work as shown below:

library(ggplot2)
library(aplot)
options("aplot_guides" = "kepp")

a <- data.frame(x=rnorm(100),y=rnorm(100),type=rep(c("a","b"),each=50))

p <- ggplot(a,aes(x=x,y=y,fill=type)) + geom_point() + theme(legend.position = "none")

p_top <- ggplot(a,aes(x=x,fill=type)) + geom_density(alpha=0.5) +
    theme(legend.position = c(0.9,0.9))

p_right <- ggplot(a,aes(y=y,fill=type)) + geom_density(alpha=0.5) +
    theme(legend.position = "none")

ap <- p %>%
    insert_top(p_top, height=.3) %>% 
    insert_right(p_right, width=.3)
> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.6

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] aplot_0.1.0.994      cowplot_1.1.1        ggpubr_0.4.0         forcats_0.5.1       
 [5] stringr_1.4.0        dplyr_1.0.7          purrr_0.3.4          readr_2.0.1         
 [9] tidyr_1.1.3          tibble_3.1.4         ggplot2_3.3.5        tidyverse_1.3.1     
[13] ComplexHeatmap_2.8.0

image

GuangchuangYu commented 3 years ago
options("aplot_guides" = "kepp")

should be:

options("aplot_guides" = "keep")
wenbostar commented 3 years ago

I already tried this:

options("aplot_guides" = "keep")
library(ggplot2)
library(aplot)

a <- data.frame(x=rnorm(100),y=rnorm(100),type=rep(c("a","b"),each=50))

p <- ggplot(a,aes(x=x,y=y,fill=type)) + geom_point() + theme(legend.position = "none")

p_top <- ggplot(a,aes(x=x,fill=type)) + geom_density(alpha=0.5) +
    theme(legend.position = c(0.9,0.9))

p_right <- ggplot(a,aes(y=y,fill=type)) + geom_density(alpha=0.5) +
    theme(legend.position = "none")

ap <- p %>%
    insert_top(p_top, height=.3) %>% 
    insert_right(p_right, width=.3)

It's the same: image

wenbostar commented 3 years ago

@GuangchuangYu , any suggestion on how to keep the legend not changed for each subplot?

wenbostar commented 2 years ago

The latest version works very nice.