代码之家  ›  专栏  ›  技术社区  ›  Mark

用r shining中的对数刻度减少复杂散点图中的网格线数量

  •  1
  • Mark  · 技术社区  · 7 年前

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

    我想找到一种既适用于二维又适用于三维平面散点图的方法,因为我同时使用这两种方法。

    这是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))))
        })
      }
        )
    

    相同,但在二维

            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])), 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, 
                                             ticks="outside",
                                             tickvals=tval,
                                             ticktext=ttxt),
                              xaxis = list(type="log",
                                           zeroline=F, showline=T, 
                                           ticks="outside",
                                           tickvals=tval,
                                           ticktext=ttxt))
                })
              }
    
    
      )
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   dww Jarretinha    7 年前

    对于二维散点图,可以使用 shapes 选择权 layout . 然后还可以使用 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))
        })
      }
    )
    

    enter image description here

    不幸的是,我认为你不能在三维绘图中使用这种方法,因为形状没有Z维。你可以用类似的方法 add_lines 而不是形状,但这不会很整齐。