在水平绘图子图中共享x轴标签

时间:2019-02-22 19:45:09

标签: r plotly subplot

我有一个用R的{​​{1}}创建的图形列表,例如:

plotly

我想水平放置,其中只有一个共享的x轴标签和一个共享的y轴标签。

我正在使用:

library(plotly)
library(dplyr)

set.seed(1)
data.df <- data.frame(val = c(rnorm(100,0,1),rnorm(100,1,1)), group = c(rep("A",100),rep("B",100)))
density.df <- do.call(rbind,lapply(levels(data.df$group),function(g){
  dens <- density(dplyr::filter(data.df,group == g)$val)
  data.frame(x = dens$x, y = dens$y, group = g)
}))

plot.list <- lapply(1:5,function(x)
  plot_ly(x = density.df$x, y = density.df$y, type = 'scatter', mode = 'lines',color = density.df$group, showlegend = (x == 5)) %>%
    layout(xaxis = list(title= "Value", zeroline = F), yaxis = list(title = "Density", zeroline = F))
  )

并得到:

enter image description here

是否不可能在水平图中获得单个x轴标签?

如果我将plotly::subplot(plot.list, nrows = 1, shareX = T, shareY = T, titleX = T, titleY = T) 参数值从1更改为5,y轴标签也会发生同样的情况。

1 个答案:

答案 0 :(得分:0)

您可以手动添加注释

https://community.plot.ly/t/subplots-how-to-add-master-axis-titles/13927/5

myplotly(df) %>%
    add_annotations(
        text = "my x title",
        x = 0.5,
        y = 0,
        yref = "paper",
        xref = "paper",
        xanchor = "center",
        yanchor = "bottom",
        yshift = -35,
        showarrow = FALSE,
        font = list(size = 15)
    )