使用 R plotly subplot 避免重复图例会导致缺失点
Avoiding duplicate legends with R plotly subplot results in missing points
更新了更完整的示例
与 相关,我正在使用 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
也许有人会用 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")))
这有帮助吗?我认为问题出在您指定的方式 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)
请注意,如果您使用 add_markers
,则不需要 mode = 'markers'
(注意复数形式)。但是,如果您使用更通用的 add_trace
.
,则它是必需的
更新了更完整的示例
与 相关,我正在使用 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
也许有人会用 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")))
这有帮助吗?我认为问题出在您指定的方式 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)
请注意,如果您使用 add_markers
,则不需要 mode = 'markers'
(注意复数形式)。但是,如果您使用更通用的 add_trace
.