R-与yaxis值重叠的第二个轴标签

R-与yaxis值重叠的第二个轴标签,r,shiny,plotly,R,Shiny,Plotly,我正在使用R,shiny和plotly尝试构建一个交互式用户界面。基本上,我有一个数据集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',marke

我正在使用R,shiny和plotly尝试构建一个交互式用户界面。基本上,我有一个数据集
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')
这里,因为我想把图例放在底部。但是,如果我这样做,右侧的第二个轴值与标签重叠,并且仅显示部分数字,例如,它显示的是5,而不是59


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

你可以在yaxis2中使用
automargin=T

试试这个:

# 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
         )
#虚拟数据
种子集(123)
数据集=数据帧(日期=序号日期(从=截止日期(“2016-07-05”),到=截止日期(“2020-01-05”),按=“月”),
价格=c(57,59.5,标准差(n=41,平均值=58.75,标准差=0.1))
)
#亚西2号公路平面布置图
ay%
绘图(x=~Date,y=~Price,type='scatter',mode='marker+lines',marker=list(size=10),name=“A”)%>%
添加跟踪(x=~Date,y=~Price,name=“B”,yaxis=“y2”)%>%
布局(图例=列表(方向='h'),
标题=‘商品价格趋势’,
yaxis2=ay
)
这里是输出图:

也许玩边距游戏效果很好!有一个名为
margin
的参数,用户可以自定义。
# 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
         )