【问题标题】:Shiny: How to add a median line on a box plot using Plotly?Shiny:如何使用 Plotly 在箱形图上添加中线?
【发布时间】:2020-05-28 16:49:31
【问题描述】:

我在闪亮的网络应用程序中做这个项目。

我想在箱形图上添加一条中线。

这是我的代码

library(shiny)
library(plotly)
ui <- fluidPage(
        mainPanel(
            plotlyOutput("distPlot")
        )
)

server <- function(input, output) {

    output$distPlot <- renderPlotly({
        date <- c("1990-01-13",
               "1990-01-13",
               "1990-01-13",
               "1990-01-13",
               "1990-01-13",
               "1990-01-13",
               "1990-01-13",
               "1990-01-14",
               "1990-01-14",
               "1990-01-14",
               "1990-01-14",
               "1990-01-14",
               "1990-01-14",
               "1990-01-14",
               "1990-01-15",
               "1990-01-15",
               "1990-01-16",
               "1990-01-16",
               "1990-01-16",
               "1990-01-16",
               "1990-01-16",
               "1990-01-16",
               "1990-01-17",
               "1990-01-17",
               "1990-01-17",
               "1990-01-18",
               "1990-01-18",
               "1990-01-18",
               "1990-01-18",
               "1990-01-18",
               "1990-01-18",
               "1990-01-19",
               "1990-01-19",
               "1990-01-19",
               "1990-01-19"

        )
        trend  <- c("9.5",
               "6.2",
               "3.3",
               "3.6",
               "6.1",
               "0.6",
               "2.3",
               "2.3",
               "4.7",
               "9.9",
               "12",
               "4.6",
               "4.5",
               "8",
               "2.3",
               "3.4",
               "7.7",
               "1.9",
               "2",
               "10.1",
               "3.3",
               "4.7",
               "6.5",
               "3.9",
               "4.4",
               "5.2",
               "8.1",
               "3.2",
               "6.3",
               "4.4",
               "2.4",
               "0.5",
               "7.2",
               "8.1",
               "5.9"
        )
        mydata <- data.frame(trend ,date())
        p <- plot_ly(mydata,x=date,y = trend  , type = "box",showlegend = FALSE)%>%
            layout(yaxis = list(title = 'trend')
                   )
        p
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

实际上我是从 csv 文件中导入数据(趋势和日期)。但我不知道如何在 stackoverflow 中上传文件,所以我认为创建数据框更适合重现。

在我的预期中。

我的阴谋

[再次创建以进行复制]

当我在我的数据中使用聚合时

【问题讨论】:

  • 顺便说一句:您可以使用dput(&lt;your_variable_name&gt;)的输出将数据复制到stackoverflow
  • 好的,我正在尝试。
  • 刚刚看到你的earlier question。区别在哪里 - shiny 上下文?
  • 我使用 read.csv.sql 。 shiny 仅适用于 query 中的 where 子句。我认为我之前的问题不好,它只有随机数据。
  • 是的,你的答案可以做到。但我遇到了一些问题,我会更新有问题的

标签: r shiny plotly boxplot r-plotly


【解决方案1】:

请检查以下内容:

library(shiny)
library(plotly)

mydata <- data.frame(trend = c(9.5, 6.2, 3.3, 3.6, 6.1, 0.6, 2.3, 2.3, 4.7, 9.9, 12, 4.6, 4.5, 8, 2.3, 3.4, 7.7, 1.9, 2, 10.1, 3.3, 4.7, 6.5, 3.9, 4.4, 5.2, 8.1, 3.2, 6.3, 4.4, 2.4, 0.5, 7.2, 8.1, 5.9),
                     date = c("1990-01-13",  "1990-01-13",  "1990-01-13",  "1990-01-13",  "1990-01-13",  "1990-01-13",  "1990-01-13",  "1990-01-14",  "1990-01-14",  "1990-01-14",  "1990-01-14",  "1990-01-14",  "1990-01-14",  "1990-01-14",  "1990-01-15",  "1990-01-15",  "1990-01-16",  "1990-01-16",  "1990-01-16",  "1990-01-16",  "1990-01-16",  "1990-01-16",  "1990-01-17",  "1990-01-17",  "1990-01-17",  "1990-01-18",  "1990-01-18",  "1990-01-18",  "1990-01-18",  "1990-01-18",  "1990-01-18",  "1990-01-19",  "1990-01-19",  "1990-01-19",  "1990-01-19"))
mymediandata <- aggregate(trend ~ date, data = mydata, median)

ui <- fluidPage(mainPanel(plotlyOutput("distPlot")))

server <- function(input, output) {
  output$distPlot <- renderPlotly({
    p <-
      plot_ly(
        mydata,
        x = ~ date,
        y = ~ trend,
        type = "box",
        showlegend = FALSE
      ) %>% add_lines(data = mymediandata, x = ~ date, y = ~ trend) %>%
      layout(yaxis = list(title = 'trend'))
    p
  })
}

# Run the application
shinyApp(ui = ui, server = server)

【讨论】:

  • ~ 后面的= 重要吗?
  • 请看this
  • 好的,我明白了。 = ~ 如此重要。现在我可以画中线了。
猜你喜欢
  • 2020-05-22
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
相关资源
最近更新 更多