r - 我可以放大一个图并让其他图也以同样的方式缩放吗? (ggplot 和plotly)

标签 r ggplot2 plotly zooming

可重现

查看以下可重现的示例:

# 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)

这是结果:

enter image description here

当我在两个图中之一放大 Y 轴时,另一幅图会相应更新,但是如果我放大 X 轴,则只有我正在处理的图会发生变化。

问题

当我这样做时:

enter image description here

我明白了:

enter image description here

请注意,右侧的 X 轴没有改变。 shareX = TRUE 并不能解决问题。

如何解决?

最佳答案

您可以对每个ggplotly使用matcheslayout来自动更改两个图表上的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

这里是一个例子:

enter image description here

关于r - 我可以放大一个图并让其他图也以同样的方式缩放吗? (ggplot 和plotly),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75410819/

相关文章:

python - 如何将 R 数据帧导入 Pandas?

performance - 提高可视化重叠段的性能

python - 相当于Python中ggplot2的rpy2中的scale_size_area?

python - 从命令行运行 Plotly 脚本

r - 如何创建 "nested"条形图?

r - R 是否在分配时复制 S4 类(class)中未评估的插槽?

r - 使用 ggalluvial 包将标签数据添加到桑基绘图轴

r - ggplot 中的双点

python - 在绘图中绘制热图时,我的 Y 轴未显示 Y 轴上的所有日期

python - 使用 plotly.express 折线图,线条以奇怪的顺序连接?怎么修?