完全缩放Y在子图行上不起作用 [英] Plotly scaleY not working across subplot rows
问题描述
建立在另一个问题上(如何删除重复的图例w与plotly subplots()),我面临着一个新问题.我希望两行中的所有图都具有相同的Y轴.但是,如果我将"shareY = TRUE"设为"on",则上一行的图共享一个轴,而下一行的图共享一个轴,但该轴彼此不同.
Building up on another question (How to remove duplicate legend entries w/ plotly subplots()), I am facing a new problem. I want all plots in both rows to have the same Y-axis. However, If I turn "shareY = TRUE", the plots on the upper row share an axis, and the plots on the lower row do, but the axis differ from one another.
该代码基本上是@Joris Chau的回答,但在最后一行添加了"shareY = TRUE".
The code is basically the one from the answer by @Joris Chau, but added "shareY = TRUE" on the last line.
library(plotly)
library(tidyverse)
mpg %>%
mutate_at("trans", as.factor) %>%
group_by(class) %>%
group_map(.f = ~{
## fill missing levels w/ displ = 0, cyl = first available value
complete(.x, trans, fill = list(displ = 0, cyl = head(.x$cyl, 1))) %>%
plot_ly(x = ~cyl, y = ~displ, color = ~trans, colors = "Paired", type = "bar",
showlegend = (.y == "2seater"), legendgroup = ~trans) %>%
layout(yaxis = list(title = as.character(.y)), barmode = "stack")
}) %>%
subplot(nrows = 2, shareX = TRUE, shareY = TRUE, titleY = TRUE)
我该如何指示在所有地块上使用相同的比例尺?
How can I tell plotly to use the same scale across all plots?
推荐答案
您应手动定义yaxis
的范围.在这里,我使用了c(0,ceiling(max(aggregate(displ ~ cyl+class, mpg, sum)$displ)/10)*10))
.
You should define range of yaxis
manually. Here, I used c(0,ceiling(max(aggregate(displ ~ cyl+class, mpg, sum)$displ)/10)*10))
.
aggregate(displ ~ cyl+class, mpg, sum)$displ
获取按cyl + class
分组的displ
的总和.
aggregate(displ ~ cyl+class, mpg, sum)$displ
gets the summation of displ
grouped by cyl + class
.
然后我得到最大值,最后我使用ceiling
将其四舍五入.
Then I get its maximum and at the end I round it up using ceiling
.
library(plotly)
library(tidyverse)
mpg %>%
mutate_at("trans", as.factor) %>%
group_by(class) %>%
group_map(.f = ~{
complete(.x, trans, fill = list(displ = 0, cyl = head(.x$cyl, 1))) %>%
plot_ly(x = ~cyl, y = ~displ, color = ~trans, colors = "Paired", type = "bar",
showlegend = (.y == "2seater"), legendgroup = ~trans) %>%
layout(yaxis = list(title = as.character(.y),
range=c(0, ceiling(max(
aggregate(displ~cyl+class, mpg, sum)$displ)/10)*10)),
barmode = "stack")
}) %>%
subplot(nrows = 2, shareX = TRUE, shareY = FALSE, titleY = TRUE, margin = 0.05)
这篇关于完全缩放Y在子图行上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!