【问题标题】:Showing all plot titles in plotly::subplot在 plotly::subplot 中显示所有情节标题
【发布时间】:2018-07-02 20:01:46
【问题描述】:

我有两个由Rplotly 创建的密度图:

set.seed(1)

dens.1 <- density(runif(1000,0,100))
dens.2 <- density(runif(1000,100,10000))

df.1 <- data.frame(x=dens.1$x,y=dens.1$y)
df.2 <- data.frame(x=dens.2$x,y=dens.2$y)

library(plotly)

pl.1 <- plot_ly(x=~df.1$x,y=~df.1$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
  layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
  layout(title="Data1")

pl.2 <- plot_ly(x=~df.2$x,y=~df.2$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
  layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
  layout(title="Data2")

现在,我想将它们绘制在一起。所以我用了plotlysubplot

subplot(list(pl.1,pl.2),nrows=1,shareX=F,shareY=F,titleX=T,titleY=T) %>% layout(showlegend=F)

但这只保留了pl.2的情节标题:

我如何获得该情节上的两个标题?

【问题讨论】:

    标签: r plotly subplot r-plotly


    【解决方案1】:

    要得到你想要的,根据这个 (https://rpubs.com/bcd/subplot-titles) 你可以使用:

    a <- list(
      text = "Data 1",
      font = f,
      xref = "paper",
      yref = "paper",
      yanchor = "bottom",
      xanchor = "center",
      align = "center",
      x = 0.5,
      y = 1,
      showarrow = FALSE
    )
    
    b <- list(
      text = "Data 2",
      font = f,
      xref = "paper",
      yref = "paper",
      yanchor = "bottom",
      xanchor = "center",
      align = "center",
      x = 0.5,
      y = 1,
      showarrow = FALSE
    )
    
    pl.1 <- plot_ly(x=~df.1$x,y=~df.1$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
      layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
      layout(annotations = a)
    
    pl.2 <- plot_ly(x=~df.2$x,y=~df.2$y,type='scatter',mode='lines',line=list(color="#A9A9A9")) %>%
      layout(xaxis=list(title="Count",zeroline=F),yaxis=list(title="Density",zeroline=F)) %>%
      layout(annotations = b)
    
    subplot(list(pl.1,pl.2),nrows=1,shareX=F,shareY=F,titleX=T,titleY=T) %>% layout(showlegend=F)
    

    【讨论】:

      猜你喜欢
      • 2021-10-18
      • 2021-10-18
      • 2020-02-02
      • 2021-12-06
      • 1970-01-01
      • 2019-05-22
      • 2017-11-07
      • 1970-01-01
      • 2021-06-13
      相关资源
      最近更新 更多