如何在 R 中为 Plotly 子图添加图边界?

How do you add plot borders to Plotly subplots in R?

我正在尝试制作一个具有共享轴的绘图网格,我希望每个子绘图都有绘图边框(这是可以接受的,但不理想,因为整个绘图区域都有边框)。我无法完成这项工作,结果让我觉得这在 Plotly 中可能是不可能的。以下是我尝试过的三种变体以及结果。

    library(plotly)
    library(magrittr)

    set.seed(0)
    x <- seq(from=0, to=9, by=1)
    y1 <- rnorm(10)
    y2 <- rnorm(10)
    y3 <- rnorm(10)
    y4 <- rnorm(10)

    # Attempt 1
    p1 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y1)

    p2 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y2)

    p3 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y3)

    p4 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y4)

    p <- subplot(p1, p2, p3, p4,
                 nrows = 2, shareX = TRUE, shareY = TRUE) %>%
      layout(title='Attempt 1', xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    # Attempt 2
    p1 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y1) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p2 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y2) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p3 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y3) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p4 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y4) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p <- subplot(p1, p2, p3, p4,
                 nrows = 2, shareX = TRUE, shareY = TRUE) %>%
      layout(title='Attempt 2')

    # Attempt 3
    p1 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y1) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p2 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y2) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p3 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y3) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p4 <- plot_ly(showlegend=FALSE) %>%
      add_markers(x = x, y = y4) %>%
      layout(xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

    p <- subplot(p1, p2, p3, p4,
                 nrows = 2, shareX = TRUE, shareY = TRUE) %>%
      layout(title='Attempt 3', xaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'),
             yaxis = list(showline = TRUE, mirror = TRUE, linecolor = 'black'))

我相信有人会给你一个纯粹的 plotly 解决方案,但这里有一个解决方案,我们可以创建 ggplot 对象,然后转换为 plotly

library(plotly)
library(tidyverse)

set.seed(0)
x <- seq(from=0, to=9, by=1)
y1 <- rnorm(10)
y2 <- rnorm(10)
y3 <- rnorm(10)
y4 <- rnorm(10)

p1 <- 
  {ggplot(tibble(x, y1), aes(x,y1))+
  geom_point(color = "blue")+
  labs(x='', y='')+
  theme_bw()+
  theme(panel.border = element_rect(color = "black"))} %>%
  ggplotly()

p2 <- 
  {ggplot(tibble(x, y2), aes(x,y2))+
  geom_point(color = "orange")+
  labs(x='', y='')+
  theme_bw()+
  theme(panel.border = element_rect(color = "black"))} %>%
  ggplotly()

p3 <- 
  {ggplot(tibble(x, y3), aes(x,y3))+
  geom_point(color = "green")+
  labs(x='', y='')+
  theme_bw()+
  theme(panel.border = element_rect(color = "black"))} %>%
  ggplotly()

p4 <- 
  {ggplot(tibble(x, y4), aes(x,y4))+
  geom_point(color = "red")+
  labs(x='', y='')+
  theme_bw()+
  theme(panel.border = element_rect(color = "black"))} %>%
  ggplotly()


subplot(p1, p2, p3, p4,nrows = 2, shareX = TRUE, shareY = TRUE)

如果您在布局属性中为每个地块提供相同的 range,您的单独边界线将被保留。

以下是我建议的定义范围的方法:

#find the max and min Y, which you will use as your range values
your_Ys<-c(y1,y2,y3,y4)
max_y<-ceiling(max(your_Ys))
min_y<-floor(min(your_Ys))

我在这里定义 x 和 Y 属性,而不是在每个图中制作属性列表:

#These are the layout attributes for Y
ay <- list(
  showline = TRUE,
  mirror = "ticks",
  linecolor = toRGB("black"),
  linewidth = 2,
  range = c(min_y, max_y)
)

#These are the layout attributes for X
ax <- list(
  showline = TRUE,
  mirror = "ticks",
  linecolor = toRGB("black"),
  linewidth = 2,
  range = c(-1, 10)
)

现在是时候把它们放在一起了。

p1 <- plot_ly(showlegend=FALSE) %>%
  add_markers(x = x, y = y1)  %>% layout( xaxis = ax, yaxis = ay)

p2 <- plot_ly(showlegend=FALSE) %>%
  add_markers(x = x, y = y2)  %>% layout( xaxis = ax, yaxis = ay)

p3 <- plot_ly(showlegend=FALSE)%>%
  add_markers(x = x, y = y3) %>%layout( xaxis = ax, yaxis = ay)

p4 <- plot_ly(showlegend=FALSE) %>%
  add_markers(x = x, y = y4)%>% layout( xaxis = ax, yaxis = ay)

p <- subplot(p1, p2, p3, p4,
             nrows = 2, shareX = FALSE, shareY = FALSE) %>%
  layout(title='Tada!')
p

设置 shareX 和 shareY = FALSE 以保留边框。

N.B。如果您也在 SEAnalyst 提供的代码中设置 shareX 或 shareY = TRUE,您会看到一些边框也没有保留。