闪亮:如何使用Plotly在箱形图上添加中线?

时间:2020-02-13 03:30:47

标签: r shiny plotly boxplot r-plotly

我在闪亮的Web应用程序中执行此项目。

我想在箱形图中添加一条中线的痕迹。

这是我的代码

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中上传文件,因此我认为创建数据帧更适合重现。

符合我的预期。

enter image description here

我的情节

enter image description here

[再次创建以进行复制]

当我使用汇总到我的数据中时

enter image description here

1 个答案:

答案 0 :(得分: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)

enter image description here