R-闪亮的第二轴标签与 y 轴值重叠

use*_*612 3 r shiny plotly

我正在使用 R,闪亮且阴谋地尝试构建一个交互式用户界面。基本上,我有一个dest包含两列Date和 的数据集price。这是一个基本的线图:

ay <- list(
showticklabels = TRUE,
overlaying = "y",
side = "right",
title = "Benchmark price")

p<-plot_ly(dset, x = ~Date,y= ~Price,type = 'scatter',mode ='lines',marker=list(size = 10),name=paste0(input$select_bench," as of ",input$benchdate)) %>% layout(xaxis = ax, yaxis2 =ay)

p<-add_trace(p,x=~bDate,y=~bPrice,type = 'scatter',mode = 'lines',marker=list(size = 10),name=paste0(input$select_bench," as of ",input$benchdate),textposition = 'middle right',yaxis="y2")}

layout(p,legend = list(orientation = 'h'),title = 'Commodity Price Trending')
Run Code Online (Sandbox Code Playgroud)

我正在使用

legend = list(orientation = 'h')
Run Code Online (Sandbox Code Playgroud)

因为我想把图例放在底部。但是,如果我这样做,右侧的第二个轴值将与标签重叠,并且仅显示数字的一部分,例如,它显示 5 而不是 59。

我认为应该有一个参数来调整显示区域的默认边距 - 但尝试用谷歌搜索没有找到任何东西。

vpz*_*vpz 6

您可以automargin = T在 yaxis2 中使用。

尝试这个:

# Dummy data

set.seed(123)

dset = data.frame(Date = seq.Date(from = as.Date("2016-07-05"),to = as.Date("2020-01-05"), by = "month"),
                  Price = c(57,59.5,rnorm(n = 41, mean = 58.75, sd = .1))
                  )

# layout of yaxis2
ay <- list( showticklabels = TRUE,
            overlaying = "y",
            side = "right",
            title = "Benchmark price",
            automargin = T # This do the trick
            )

# Example of your plot
dset %>%
  plot_ly(x = ~Date,y= ~Price,type = 'scatter',mode ='marker+lines',marker=list(size = 10), name = "A") %>%
  add_trace(x = ~Date, y = ~Price , name = "B", yaxis = "y2") %>%
  layout(legend = list(orientation = 'h'),
         title = 'Commodity Price Trending',
         yaxis2 = ay
         )
Run Code Online (Sandbox Code Playgroud)

这里是输出图: 阴谋