gpt4 book ai didi

R- shiny-plotly 第二轴标签与 yaxis 值重叠

转载 作者:行者123 更新时间:2023-12-05 02:19:32 25 4
gpt4 key购买 nike

我正在使用 R,闪闪发亮地尝试构建交互式用户界面。基本上,我有一个数据集 dest,它有两列 Dateprice。这是一个基本的线图:

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')

我正在使用

legend = list(orientation = 'h')

这里是因为我想把图例放在底部。但是,如果我这样做,右边的第二个轴值会与标签重叠,并且只显示部分数字,例如它显示 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
)

这里是输出图: plot

关于R- shiny-plotly 第二轴标签与 yaxis 值重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42499647/

25 4 0