【问题标题】:how to remove Trace 0 and Trace 1 in plotly line in Rshiny?如何在 R Shiny 的情节线中删除 Trace 0 和 Trace 1?
【发布时间】:2021-02-16 20:13:18
【问题描述】:

在 Rshiny 中,线图在图的右上角显示迹线 0 和迹线 1。单击第 0 行使该行消失,而单击 Trace 1 使该行顶部的文本消失。代码如下。

ui <- fluidPage(plotlyOutput("fig1"))
server <- function(input, output) {
  
  Primates <- c('Potar monkey', 'Gorilla', 'Dinosaur', 'Rhesus monkey', 'Chimp')
  Bodywt <- c('a', 'b', 'c', 'd', 'e')
  Brainwt <- c(115, 406, 1320, 179, 440)
  data <- data.frame(Primates, Bodywt, Brainwt)
  
  fig <- plot_ly(data, x = ~Bodywt, y = ~Brainwt, type = 'scatter', mode = 'lines')
  fig <- fig %>% add_text(text = data$Primates, textposition = 'top', textfont = list(color = '#000000', size = 16))
  fig <- fig %>% layout(title = 'Primates Brain and Body Weight',
                        xaxis = list(title = 'Body Weight (kg)',
                                     zeroline = TRUE),
                        yaxis = list(title = 'Brain Weight (g)',
                                     range = c(0,1400)))
  
  output$fig1 <- renderPlotly(fig)
  
}

shinyApp(ui,server)

我需要一些帮助来删除 Trace 0 和 Trace 1。

【问题讨论】:

  • 您要删除图例还是链接图例项(两条迹线并行隐藏)?

标签: r shiny plotly trace r-plotly


【解决方案1】:

您可以使用'lines+text'模式创建组合跟踪:

library(shiny)
library(plotly)

ui <- fluidPage(plotlyOutput("fig1"))
server <- function(input, output) {
  
  Primates <- c('Potar monkey', 'Gorilla', 'Dinosaur', 'Rhesus monkey', 'Chimp')
  Bodywt <- c('a', 'b', 'c', 'd', 'e')
  Brainwt <- c(115, 406, 1320, 179, 440)
  data <- data.frame(Primates, Bodywt, Brainwt)
  
  fig <- plot_ly(data, x = ~Bodywt, y = ~Brainwt, type = 'scatter', mode = 'lines+text', text = ~Primates, textposition = 'top', textfont = list(color = '#000000', size = 16))
  fig <- fig %>% layout(showlegend = TRUE,
                        title = 'Primates Brain and Body Weight',
                        xaxis = list(title = 'Body Weight (kg)',
                                     zeroline = TRUE),
                        yaxis = list(title = 'Brain Weight (g)',
                                     range = c(0,1400)))
  
  output$fig1 <- renderPlotly(fig)
  
}

shinyApp(ui,server)

【讨论】:

    【解决方案2】:

    我找到了另一种解决方法。我们还可以在布局中添加 showlegend = F 来避免 Trace0 和 Trace1 出现。

    【讨论】:

    • 让我猜猜,你在我的回答中“找到”了它。我在cmets问了你为什么不澄清?
    猜你喜欢
    • 2016-12-15
    • 2019-04-16
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 2021-10-21
    • 2014-09-05
    • 2015-06-15
    • 1970-01-01
    相关资源
    最近更新 更多