R图示共享图例方向

时间:2018-06-16 07:01:27

标签: r plotly legend r-plotly

我尝试使用list合并R plotly plotly::subplot个数字,并且我希望合并后的图表能够显示一次图例,水平定向,在顶部。

这是我的数据:

set.seed(1)
df <- data.frame(val=rnorm(10*200),
                 group=rep(c(rep("a",100),rep("b",100)),10),
                 cluster=unlist(lapply(1:10,function(l) rep(paste0("cluster:",l),200))))

代码:

library(plotly)
library(dplyr)


plot.list <- lapply(unique(df$cluster),function(l){
  cluster.df <- df %>% dplyr::filter(cluster == l)
  if(l == unique(df$cluster)[1]){
    c.plot <- plot_ly(x=~cluster.df$group,y=~cluster.df$val,split=~cluster.df$group,type='violin',box=list(visible=T),meanline=list(visible=T),showlegend=T,color=~cluster.df$group,legendgroup=l) %>%
      layout(xaxis=list(title=NA,zeroline=F,tickangle=90,tickvals=0.5,ticktext=l),yaxis=list(title="Value",zeroline=F),legend=list(orientation="h",yanchor="top",x=0.5,y=1))
  } else{
    c.plot <- plot_ly(x=~cluster.df$group,y=~cluster.df$val,split=~cluster.df$group,type='violin',box=list(visible=T),meanline=list(visible=T),showlegend=F,color=~cluster.df$group,legendgroup=l) %>%
      layout(xaxis=list(title=NA,zeroline=F,tickangle=90,tickvals=0.5,ticktext=l),yaxis=list(title="Value",zeroline=F))
  }
  return(c.plot)
})

然后,

subplot(plot.list,nrows=1,shareX=T,shareY=T,margin=0.01)

给出: enter image description here

所以一切看起来都不错,除非指定:

,否则共享图例不会水平定向
legend=list(orientation="h",yanchor="top",x=0.5,y=1)

如果我尝试仅使用相同的绘图代码绘制第一个cluster

cluster.df <- df %>% dplyr::filter(cluster == "cluster:1")
plot_ly(x=~cluster.df$group,y=~cluster.df$val,split=~cluster.df$group,type='violin',box=list(visible=T),meanline=list(visible=T),showlegend=T,color=~cluster.df$group,legendgroup="cluster:1") %>%
  layout(xaxis=list(title=NA,zeroline=F,tickangle=90,tickvals=0.5,ticktext=l),yaxis=list(title="Value",zeroline=F),legend=list(orientation="h",yanchor="top",x=0.5,y=1))

我明白了:

enter image description here

但如果我放弃legendgroup="cluster:1"

plot_ly(x=~cluster.df$group,y=~cluster.df$val,split=~cluster.df$group,type='violin',box=list(visible=T),meanline=list(visible=T),showlegend=T,color=~cluster.df$group) %>%
      layout(xaxis=list(title=NA,zeroline=F,tickangle=90,tickvals=0.5,ticktext=l),yaxis=list(title="Value",zeroline=F),legend=list(orientation="h",yanchor="top",x=0.5,y=1))

我明白了:

enter image description here

所以似乎代码的legendgroup部分干扰了图例方向。

知道怎么做对吗?

这是我的sessioInfo()

> sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.5

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/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      parallel  stats4    stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] dplyr_0.7.5          bindrcpp_0.2.2       plotly_4.7.1.9000    ggplot2_2.2.1.9000  
 [5] Gviz_1.22.3          GenomicRanges_1.30.2 GenomeInfoDb_1.14.0  IRanges_2.12.0      
 [9] S4Vectors_0.16.0     BiocGenerics_0.24.0 

loaded via a namespace (and not attached):
 [1] ProtGenerics_1.10.0           bitops_1.0-6                  matrixStats_0.53.1           
 [4] bit64_0.9-7                   RColorBrewer_1.1-2            progress_1.1.2               
 [7] httr_1.3.1                    tools_3.4.3                   backports_1.1.2              
[10] R6_2.2.2                      rpart_4.1-11                  Hmisc_4.1-1                  
[13] DBI_0.7                       lazyeval_0.2.1                colorspace_1.3-2             
[16] nnet_7.3-12                   withr_2.1.2                   tidyselect_0.2.4             
[19] gridExtra_2.3                 prettyunits_1.0.2             RMySQL_0.10.13               
[22] curl_3.1                      bit_1.1-12                    compiler_3.4.3               
[25] Biobase_2.38.0                htmlTable_1.11.2              DelayedArray_0.4.1           
[28] rtracklayer_1.38.3            scales_0.5.0                  checkmate_1.8.5              
[31] stringr_1.3.1                 digest_0.6.15                 Rsamtools_1.30.0             
[34] foreign_0.8-69                XVector_0.18.0                base64enc_0.1-3              
[37] dichromat_2.0-0               pkgconfig_2.0.1               htmltools_0.3.6              
[40] ensembldb_2.2.2               BSgenome_1.46.0               htmlwidgets_1.2              
[43] rlang_0.2.1                   rstudioapi_0.7                RSQLite_2.0                  
[46] BiocInstaller_1.28.0          shiny_1.0.5                   bindr_0.1.1                  
[49] jsonlite_1.5                  crosstalk_1.0.0               BiocParallel_1.12.0          
[52] acepack_1.4.1                 VariantAnnotation_1.24.5      RCurl_1.95-4.10              
[55] magrittr_1.5                  GenomeInfoDbData_1.0.0        Formula_1.2-2                
[58] Matrix_1.2-12                 Rcpp_0.12.17                  munsell_0.4.3                
[61] yaml_2.1.18                   stringi_1.2.2                 SummarizedExperiment_1.8.1   
[64] zlibbioc_1.24.0               AnnotationHub_2.10.1          plyr_1.8.4                   
[67] blob_1.1.0                    lattice_0.20-35               Biostrings_2.46.0            
[70] splines_3.4.3                 GenomicFeatures_1.30.3        knitr_1.19                   
[73] pillar_1.1.0                  biomaRt_2.34.2                XML_3.98-1.11                
[76] glue_1.2.0                    biovizBase_1.26.0             latticeExtra_0.6-28          
[79] data.table_1.10.4-3           httpuv_1.3.5                  gtable_0.2.0                 
[82] purrr_0.2.5                   tidyr_0.8.1                   assertthat_0.2.0             
[85] mime_0.5                      xtable_1.8-2                  AnnotationFilter_1.2.0       
[88] survival_2.41-3               viridisLite_0.3.0             tibble_1.4.2                 
[91] GenomicAlignments_1.14.1      AnnotationDbi_1.40.0          memoise_1.1.0                
[94] cluster_2.0.6                 interactiveDisplayBase_1.16.0

0 个答案:

没有答案