使用R中的对数比例减少绘图散点图中的网格线数量

使用R中的对数比例减少绘图散点图中的网格线数量,r,shiny,plotly,gridlines,R,Shiny,Plotly,Gridlines,我已经构建了以下测试应用程序,解决了将刻度标签作为科学注释的问题,但现在我想减少网格线的数量,使其仅放置在“主”刻度上,即具有文本标签的刻度。 此问题是根据之前的讨论/评论发布的 我想找到一种同时适用于二维和三维绘图散点图的方法,因为我同时使用这两种方法 这是3D应用程序。 library(shiny) library(plotly) shinyApp( ui = fluidPage( plotlyOutput('plot') ), serve

我已经构建了以下测试应用程序,解决了将刻度标签作为科学注释的问题,但现在我想减少网格线的数量,使其仅放置在“主”刻度上,即具有文本标签的刻度。 此问题是根据之前的讨论/评论发布的

我想找到一种同时适用于二维和三维绘图散点图的方法,因为我同时使用这两种方法

这是3D应用程序。

    library(shiny)
    library(plotly)

    shinyApp(
      ui = fluidPage( plotlyOutput('plot') ),

      server = function(input, output) {
        output$plot <- renderPlotly ({

          mtcars <- rbind(mtcars, mtcars*1000, mtcars/1000)  #create data with big logarithmic range
          maxlog <- round(log10(max(mtcars[['mpg']][mtcars[['mpg']]>0], mtcars[['disp']][mtcars[['disp']]>0],mtcars[['cyl']][mtcars[['cyl']]>0])), digits = 0) +1 # determine max log needed
          minlog <- round(log10(min(mtcars[['mpg']][mtcars[['mpg']]>0], mtcars[['disp']][mtcars[['disp']]>0],mtcars[['cyl']][mtcars[['cyl']]>0])), digits = 0) -1 # determine min log needed
          logrange <- (maxlog - minlog)*9 +1 # get the distance between smallest and largest log power
          tval <- sort(as.vector(sapply(seq(1,9), function(x) x*10^seq(minlog, maxlog)))) #generates a sequence of numbers in logarithmic divisions
          ttxt <- rep("",length(tval))  # no label at most of the ticks
          ttxt[seq(1,logrange,9)] <- formatC(tval, format = "e", digits = 2)[seq(1,logrange,9)] # every 9th tick is labelled


          p <- plot_ly(source = 'ThresholdScatter')
          p <- add_trace(p, data = mtcars, 
                      x = mtcars[['mpg']], 
                      y = mtcars[['disp']],
                      z = mtcars[['cyl']],
                      type = 'scatter3d', 
                      mode = 'markers',
                      marker = list(size = 2)) 

      p <- layout(p, autosize = F, width = 500, height = 500,
                  scene = list(yaxis = list(type="log",
                                            zeroline=F, showline=T, 
                                            ticks="outside",
                                            tickvals=tval,
                                            ticktext=ttxt),
                               xaxis = list(type="log",
                                            zeroline=F, showline=T, 
                                            ticks="outside",
                                            tickvals=tval,
                                            ticktext=ttxt),
                               zaxis = list(type="log",
                                            zeroline=F, showline=T, 
                                            ticks="outside",
                                            tickvals=tval,
                                            ticktext=ttxt),
                               camera = list(eye = list(x = -1.5, y = 1.5, z = 1.5))))
    })
  }
    )
库(闪亮)
图书馆(绘本)
shinyApp(
ui=fluidPage(plotlyOutput(“plot”),
服务器=功能(输入、输出){
输出$plot 0])),数字=0)+1#确定所需的最大日志
minlog 0],mtcars[['disp']][mtcars[['disp']]>0],mtcars[['cyl']]][mtcars[['cyl']]>0]),数字=0)-1#确定所需的最小日志

logrange对于二维散点图,可以使用
布局中的
形状
选项绘制自己的网格线。然后还可以使用
showgrid=FALSE
抑制网格线

shinyApp(
  ui = fluidPage( plotlyOutput('plot') ),

  server = function(input, output) {

    hline <- function(y = 0, color = "grey", width=0.1) {
      list(type = "line", x0 = 0, x1 = 1, xref = "paper",
        y0 = y, y1 = y, line = list(color = color, width=width))
    }

    output$plot <- renderPlotly ({
      mtcars <- rbind(mtcars, mtcars*1000, mtcars/1000)  #create data with big logarithmic range
      maxlog <- round(log10(max(mtcars[['mpg']][mtcars[['mpg']]>0], mtcars[['disp']][mtcars[['disp']]>0])), digits = 0) +1 # determine max log needed
      minlog <- round(log10(min(mtcars[['mpg']][mtcars[['mpg']]>0], mtcars[['disp']][mtcars[['disp']]>0])), digits = 0) -1 # determine min log needed
      logrange <- (maxlog - minlog)*9 +1 # get the distance between smallest and largest log power
      tval <- sort(as.vector(sapply(seq(1,9), function(x) x*10^seq(minlog, 

        maxlog)))) #generates a sequence of numbers in logarithmic divisions
      ttxt <- rep("",length(tval))  # no label at most of the ticks
      ttxt[seq(1,logrange,9)] <- formatC(tval, format = "e", digits = 2)[seq(1,logrange,9)] # every 9th tick is labelled

      p <- plot_ly(source = 'ThresholdScatter')
      p <- add_trace(p, data = mtcars, 
        x = mtcars[['mpg']], 
        y = mtcars[['disp']],
        type = 'scatter', 
        mode = 'markers',
        marker = list(size = 2)) 

      p <- layout(p,autosize = F, width = 500, height = 500,
        yaxis = list(type="log",
          zeroline=F, showline=T, showgrid=F,
          ticks="outside",
          tickvals=tval,
          ticktext=ttxt),
        xaxis = list(type="log",
          zeroline=F, showline=T, showgrid=F,
          ticks="outside",
          tickvals=tval,
          ticktext=ttxt),
        shapes = lapply(10^(-1:6), hline))
    })
  }
)
shinyApp(
ui=fluidPage(plotlyOutput(“plot”),
服务器=功能(输入、输出){

hline在Python中,对于3D绘图,指定
场景中的所有布局属性,如下所示:

layout = go.Layout(
        margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    ),
    scene=dict(
    xaxis=dict(
        type='log',
               autorange=True,
               title='L1'))
)
对于最新版本的plotly,我假设R中也存在相同的功能


您需要使用“场景”,请参见下面我的完整答案。如果只是使用“场景”,这是多余的,参见其他答案。不,不是伊农,看看你的场景方法如何无法生成科学注释,这就是这个问题的全部目标。伊农,我知道这种方法,是的,你也可以在R中设置对数比例,但在你的例子中,L1轴不是表示对数比例的正确科学方法。一旦你用javascript替换它这样做,每个对数步长有9行你的意思是你希望有10个小步长从1到0.1,然后从0.1到0.01,等等?如果是这样,也许这可以用“自动范围”来控制参数?理想情况下,我希望一行为整个对数刻度,大约为一半,但您的解决方案无法将它们标记为1.00+01格式。因此,请从另一个答案中删除您的否决票
layout = go.Layout(
        margin=dict(
        l=0,
        r=0,
        b=0,
        t=0
    ),
    scene=dict(
    xaxis=dict(
        type='log',
               autorange=True,
               title='L1'))
)