ggplotly 与facet_wrap(scales = 'free_y') 创建不均匀的面板宽度

问题描述 投票:0回答:1

以下代码使用 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 比例下保持相等?

r plotly ggplotly
1个回答
-1
投票

我也有同样的问题。删除 y 轴数字解决了问题;

+theme(axis.text.x = element_blank())
© www.soinside.com 2019 - 2024. All rights reserved.