2

以下代码使用 ggplotly 创建多面 Plotly 图,其中每个面具有相同的高度和宽度:

library(ggplot2)
library(plotly)

data <- expand.grid(
  measure = paste0('m', 1:6),
  x = seq(as.Date('2017-01-01'), as.Date('2017-10-01'), length.out = 10))

data$y <- runif(nrow(data))

test <- ggplot(
  aes(
    x = x,
    y = y),
  data = data) +
  geom_point() +
  facet_wrap(~measure)

g <- ggplotly(test)
print(g)

在此处输入图像描述

但是对于新的迭代,我想让 y 尺度变化,所以我添加了scales = 'free_y'. 这会导致两个问题:

每行中的中间面板变得比外部面板窄,并且数据点在面板 [2,1] 和 [2,2] 中消失,原因不明。

test <- ggplot(
  aes(
    x = x,
    y = y),
  data = data) +
  geom_point() +
  facet_wrap(~measure, scales = 'free_y')

g <- ggplotly(test)
print(g)

在此处输入图像描述

如果我更改为scales = 'free'而不是'free_y',我会返回数据点,但每行中的中间面板仍然较窄,我需要添加面板间距以防止 x 轴标签重叠:

test <- ggplot(
  aes(
    x = x,
    y = y),
  data = data) +
  geom_point() +
  facet_wrap(~measure, scales = 'free')

g <- ggplotly(test)
print(g)

在此处输入图像描述

有没有一种好方法可以使面板宽度与不同的 y 比例保持相等?

4

0 回答 0