可重现
查看以下可重现的示例:
# libraries
library(ggplot2)
library(plotly)
# data
set.seed(1)
df <- data.frame(x = rnorm(100), y = rnorm(100))
# ggplots
g1 <- g2 <- ggplotly(ggplot(df, aes(x, y)) + geom_point())
# set together
subplot(g1, g2, shareX = TRUE, shareY = TRUE)
这是结果:
当我在两个图中之一放大 Y 轴时,另一幅图会相应更新,但是如果我放大 X 轴,则只有我正在处理的图会发生变化。
问题
当我这样做时:
我明白了:
请注意,右侧的 X 轴没有改变。
shareX = TRUE
并不能解决问题。
如何解决?
最佳答案
您可以对每个ggplotly
使用matches
和layout
来自动更改两个图表上的x轴,如下所示:
# libraries
library(ggplot2)
library(plotly)
# data
set.seed(1)
df <- data.frame(x = rnorm(100), y = rnorm(100))
# ggplots
g1 <- g2 <- ggplotly(ggplot(df, aes(x, y)) + geom_point()) %>%
layout(xaxis = list(matches = "x"))
# set together
subplot(g1, g2, shareX = TRUE, shareY = TRUE)
创建于 2023 年 2 月 10 日 reprex v2.0.2
这里是一个例子:
关于r - 我可以放大一个图并让其他图也以同样的方式缩放吗? (ggplot 和plotly),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75410819/