我正在嘗試在 Shiny 中制作互動式圖形,您可以在其中使用滑塊更改雙 Y 軸圖形上的一條線的縮放值。這是基本圖的樣子
我用來在 Shiny 中繪制圖形的代碼如下,我的目標是將“coeff”變數定義為 UI 中滑塊的輸入,然后將其合并到如何計算線的值
###Shiny dual Y axis plot
library(shiny)
library(ggplot2)
ui <- fluidPage(
titlePanel("Line Graph"),
sidebarLayout(
sidebarPanel(
sliderInput("scalegvt","Scale Government Data by:", min = 0.03, max = 1.0, value = c(1.0))
),
mainPanel(
plotOutput("distPlot")
)
)
)
## Creating server logic
server <- function(input, output) {
coeff <- reactive({
req(input$scalegvt)
})
output$distPlot <- renderPlot({ #Rendering plot
compggp <- ggplot(NULL, aes(x = date, y = covid))
geom_spline(data = onsdf,
aes(x = date, y = covid, colour = "ONS Modelled Estimates"), nknots = 90, size = 1.3)
geom_spline(data = gvtdf,
aes(x = date, y = covid/coeff, colour = "Gvt Reported Positive Tests"), nknots = 90,
size = 1.3)##This is the section I want to make reactive to "coeff"##
#Stringency bars
compggp <- compggp purrr::pmap(strindf, barfunction)
#Adding aesthetics
compggp <- compggp scale_x_date(limits = as.Date(c("2020-05-03", NA )))
theme_minimal()
scale_y_continuous(labels = scales::comma)
labs(x = "Date (year - month)", y = "Covid Cases (estimated and reported)")
scale_y_continuous(labels = scales::comma, name = "Modelled Population Estimates",
sec.axis = sec_axis(~.*coeff, name = "Reported Positive Tests"))
scale_colour_manual(name = "",
values = c("ONS Modelled Estimates"="khaki4",
"Gvt Reported Positive Tests" = "darkcyan",
"Lockdown Stringency" = "red"))
compggp
})
}
# Running application
shinyApp(ui = ui, server = server)
我實際上并沒有從此代碼中收到任何錯誤訊息;滑塊和圖形都正確呈現,但只是不相互互動。
非常感謝!
uj5u.com熱心網友回復:
感謝@YBS,coeff() 解決了它
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466725.html
上一篇:散點線組合圖
