How can I create a subplot grid with Plotly in R?
如何在R中使用Plotly创建子图网格?
The official site has this nice Python example:
官方网站有这个很好的Python示例:
The python code has the option rows=2
and cols=2
, but in R the subplot
function has just the parameter nrows
, without ncols
.
python代码有选项rows = 2和cols = 2,但在R中,subplot函数只有参数nrows,没有ncols。
I tried this example in R, but nrows
do not seam to work as expected:
我在R中尝试了这个例子,但是nrows不会按预期接缝工作:
# Basic subplot
library(plotly)
p <- plot_ly(economics, x = date, y = uempmed)
subplot(p,p,p,p,
margin = 0.05,
nrows=2
) %>% layout(showlegend = FALSE)
They are in a line instead of in a grid. See the result:
它们在一条线而不是在网格中。看结果:
Here is the R suplots page for reference. Unfortunately, use ggplotly
is not a option for me, like this
这是R suplots页面供参考。不幸的是,使用ggplotly不是我的选择,就像这样
UPDATE
It was a bug. Plotly team is very fast, and it was fixed in just 3 days (check here)! Github version is already updated. Great job!
这是一个错误。 Plotly团队非常快,仅在3天内修复(点击此处查看)! Github版本已经更新。做得好!
2 个解决方案
#1
9
This seems to be a genuine bug in the way subplot()
generates the y-axis domains for the two plots. Indeed, they overlap which can easily be seen if you execute
这似乎是subplot()为两个图生成y轴域的方式中的真正错误。实际上,它们重叠,如果你执行它们很容易看到
p <- plot_ly(economics, x = date, y = uempmed)
q <- plot_ly(economics, x = date, y = unemploy)
subplot(p,q, nrows = 2)
This will produce the following plot:
这将产生以下情节:
If you take a close look at the y-axis you see that they overlap. That hints at a problem in the way subplot()
defines the domain of the y-axes of the subplot.
如果仔细观察y轴,您会发现它们重叠。这暗示了subplot()定义子图的y轴域的方式中的问题。
If we correct the domain specification of the y-axes manually (following the plotly documentation), we can solve the problem:
如果我们手动更正y轴的域规范(遵循绘图文档),我们可以解决问题:
subplot(p,q, nrows = 2) %>% layout(yaxis = list(domain = c(0, 0.48)),
yaxis2 = list(domain = c(0.52, 1)))
这会产生:
Now, if you want to reproduce the 4x4 subplot matrix similar to the Python example, you probably need to manually adjust the x-axis domains in a similar way.
现在,如果要重现类似于Python示例的4x4子图矩阵,则可能需要以类似的方式手动调整x轴域。
Since this is a bug and my solution is only a workaround, I suggest, however, that you file an issue with plotly
on GitHub.
由于这是一个错误,我的解决方案只是一种解决方法,但我建议您在GitHub上使用plotly提出问题。
#2
0
Based on this:
基于此:
p <- economics %>%
tidyr::gather(variable, value, -date) %>%
transform(id = as.integer(factor(variable))) %>%
plot_ly(x = ~date, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0("y", id)) %>%
add_lines() %>%
subplot(nrows = 5, shareX = TRUE)
#1
9
This seems to be a genuine bug in the way subplot()
generates the y-axis domains for the two plots. Indeed, they overlap which can easily be seen if you execute
这似乎是subplot()为两个图生成y轴域的方式中的真正错误。实际上,它们重叠,如果你执行它们很容易看到
p <- plot_ly(economics, x = date, y = uempmed)
q <- plot_ly(economics, x = date, y = unemploy)
subplot(p,q, nrows = 2)
This will produce the following plot:
这将产生以下情节:
If you take a close look at the y-axis you see that they overlap. That hints at a problem in the way subplot()
defines the domain of the y-axes of the subplot.
如果仔细观察y轴,您会发现它们重叠。这暗示了subplot()定义子图的y轴域的方式中的问题。
If we correct the domain specification of the y-axes manually (following the plotly documentation), we can solve the problem:
如果我们手动更正y轴的域规范(遵循绘图文档),我们可以解决问题:
subplot(p,q, nrows = 2) %>% layout(yaxis = list(domain = c(0, 0.48)),
yaxis2 = list(domain = c(0.52, 1)))
这会产生:
Now, if you want to reproduce the 4x4 subplot matrix similar to the Python example, you probably need to manually adjust the x-axis domains in a similar way.
现在,如果要重现类似于Python示例的4x4子图矩阵,则可能需要以类似的方式手动调整x轴域。
Since this is a bug and my solution is only a workaround, I suggest, however, that you file an issue with plotly
on GitHub.
由于这是一个错误,我的解决方案只是一种解决方法,但我建议您在GitHub上使用plotly提出问题。
#2
0
Based on this:
基于此:
p <- economics %>%
tidyr::gather(variable, value, -date) %>%
transform(id = as.integer(factor(variable))) %>%
plot_ly(x = ~date, y = ~value, color = ~variable, colors = "Dark2",
yaxis = ~paste0("y", id)) %>%
add_lines() %>%
subplot(nrows = 5, shareX = TRUE)