避免使用R的子图重复图例会导致丢失点

时间:2018-08-15 17:55:27

标签: plotly r-plotly

更新了更完整的示例

关于http://stackoverflow.com/questions/26939121,我正在生成一系列标记类型为R的标记类型图plotly 4.8,它们与plotly :: subplot结合在一起,并且我将图例隐藏在每个图的第一个中对组件图,以便最终图不具有重复的图例。但是在执行此操作时,对于要绘制的两个数据帧中的每一个,仅显示第一个(x,y)点(前两个图)。证明这一点的测试代码如下。

require(plotly)
set.seed(1)

a <- data.frame(x=1:3, y=1:3)
b <- data.frame(x=(1:3)+.1, y=(1:3)+.1)
xu <- runif(1000, 0, 3)
xn <- (rnorm(1000) + 3) / 2
co <- 'black'
p <- plot_ly()
pa <- add_markers(p, mode='marker',
                  data=a, x=~x, y=~y, name='j', legendgroup='j',
                  size=I(5), color=I(co),
                  showlegend=FALSE)

pb <- add_markers(p, mode='marker',
                  data=b, x=~x, y=~y, name='j', legendgroup='j',
                  size=I(5), color=I(co),
                  showlegend=TRUE)

pc <- add_histogram(p, x=~xu, name='k', color=I('black'),
                    legendgroup='k', showlegend=FALSE)
pd <- add_histogram(p, x=~xn, name='k', color=I('black'),
                    legendgroup='k', showlegend=TRUE)

plotly::subplot(pa, pb, pc, pd, shareX=TRUE, shareY=FALSE, titleX=TRUE, nrows=4)

感谢任何指针。要使点不受add_markers的输出的影响,我必须对plotly有一些基本的误解。

这是sessionInfo()的输出:

R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.1 LTS

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] bindrcpp_0.2.2 plotly_4.8.0   ggplot2_3.0.0 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18       RColorBrewer_1.1-2 pillar_1.3.0       compiler_3.5.1     later_0.7.3        plyr_1.8.4        
 [7] bindr_0.1.1        tools_3.5.1        digest_0.6.15      jsonlite_1.5       tibble_1.4.2       gtable_0.2.0      
[13] viridisLite_0.3.0  pkgconfig_2.0.2    rlang_0.2.2        shiny_1.1.0        rstudioapi_0.7     crosstalk_1.0.0   
[19] yaml_2.2.0         withr_2.1.2        dplyr_0.7.6        httr_1.3.1         htmlwidgets_1.2    grid_3.5.1        
[25] tidyselect_0.2.4   glue_1.3.0         data.table_1.11.4  R6_2.2.2           purrr_0.2.5        tidyr_0.8.1       
[31] magrittr_1.5       scales_1.0.0       promises_1.0.1     htmltools_0.3.6    assertthat_0.2.0   xtable_1.8-2      
[37] mime_0.5           colorspace_1.3-2   httpuv_1.4.5       lazyeval_0.2.1     munsell_0.5.0      crayon_1.3.4      

2 个答案:

答案 0 :(得分:1)

这有帮助吗?我认为问题出在您指定size的方式上。

require(plotly)
set.seed(1)

a <- data.frame(x=1:3, y=1:3)
b <- data.frame(x=(1:3)+.1, y=(1:3)+.1)
xu <- runif(1000, 0, 3)
xn <- (rnorm(1000) + 3) / 2
co <- 'black'
p <- plot_ly()
pa <- add_markers(p, 
                  data=a, x=~x, y=~y, name='j', legendgroup='j',
                  marker = list(size = 5), color=I(co), # attribute 'marker' controls size of points
                  showlegend=FALSE)

pb <- add_markers(p, 
                  data=b, x=~x, y=~y, name='j', legendgroup='j',
                  marker = list(size = 5), color=I(co),# attribute 'marker' controls size of points

                  showlegend=TRUE)

pc <- add_histogram(p, x=~xu, name='k', color=I('black'),
                    legendgroup='k', showlegend=FALSE)
pd <- add_histogram(p, x=~xn, name='k', color=I('black'),
                    legendgroup='k', showlegend=TRUE)

plotly::subplot(pa, pb, pc, pd, shareX=TRUE, shareY=FALSE, titleX=TRUE, nrows=4)

请注意,如果您使用的是mode = 'markers',则不需要add_markers(注意复数)。但是,如果使用更通用的add_trace,则是必需的。

enter image description here

答案 1 :(得分:0)

也许有人会用plot_ly()来回答,但是这里是ggplot2()ggplotly()的替代方法。

您可以尝试以下方法:

c <- data.frame(x=c(1, 2, 3, 1, 2, 3), y=c(1, 2, 3, 1, 2, 3), c = c("PA", "PA", "PA", "PB", "PB", "PB"), z = c(1, 1, 1, 1, 1, 1))

ggplotly(ggplot(data = c, aes(x = x, y =y)) + 
           geom_point(aes(color = as.factor(z))) + 
           facet_wrap(~ c, ncol = 1) + theme_bw() +  
           theme(
             strip.background = element_blank(),
             strip.text.x = element_blank()
           ) + 
           scale_color_manual(name = "", values = c("black")))

enter image description here