[英]Shiny: How to add a median line on a box plot using Plotly?
我在閃亮的網絡應用程序中做這個項目。
我想在我的箱線圖上添加一條中線的痕跡。
這是我的代碼
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中上傳文件,所以我認為創建數據框更適合重現。
在我的預期中。
我的陰謀
[重新創建以復制]
當我在我的數據中使用聚合時
請檢查以下內容:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.