使用R Shiny中的对数刻度减少绘图散点图中的网格线数量

编程入门 行业动态 更新时间:2024-10-19 11:52:32
本文介绍了使用R Shiny中的对数刻度减少绘图散点图中的网格线数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经构建了以下测试应用程序,在该应用程序中,我解决了该问题,以将刻度线标签作为科学注释获得,但是现在我希望将网格线的数量减少到仅放置在主要"刻度线处,即有文字标签的标签. 该问题是基于对先前 SO问题

I've build the following test app where I solve the issue to get the tick labels as scientific annotation, but I would now like to reduce the number of grid lines to only be placed at the "main" ticks, i.e. the ones that have a text label. This question was posted based on discussion / comment on this previous SO question

我想找到一种适用于2D和3D散点图的方法,因为我同时使用了这两种方法.

I would like to find a way that works for both 2D and 3D plotly scatter plots since I am using both.

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

推荐答案

对于2D散点图,可以使用layout中的shapes选项绘制自己的网格线.然后,您还可以使用showgrid = FALSE取消网格线.

For the 2D scatterplot, you can draw your own grid lines using the shapes option in layout. You also then suppress the gridlines using 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)) }) } )

不幸的是,我不认为您不能在3d图中使用此方法,因为形状没有z维度.您可以使用add_lines代替形状来做类似的事情,但这不会那么整洁.

Unfortunately, I don't think you can use this approach in the 3d plot, as shapes do not have a z dimension. You could do something similar using add_lines instead of shapes, but this won't be quite as neat.

更多推荐

使用R Shiny中的对数刻度减少绘图散点图中的网格线数量

本文发布于:2023-07-19 08:49:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对数   刻度   网格   图中   数量

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!