如何在闪亮的双 Y 轴图形线上添加交互式缩放
How to add interactive scaling on Shiny dual Y axis graph line
我正在尝试在 Shiny 中制作交互式图表,您可以在其中使用滑块更改双 Y 轴图表上其中一条线的缩放值。 Here is what the base graph looks like
我用来在 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)
我实际上并没有从这段代码中收到任何错误消息;滑块和图形都可以正确呈现,但不会相互影响。
非常感谢!
感谢@YBS,coeff()解决了
我正在尝试在 Shiny 中制作交互式图表,您可以在其中使用滑块更改双 Y 轴图表上其中一条线的缩放值。 Here is what the base graph looks like
我用来在 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)
我实际上并没有从这段代码中收到任何错误消息;滑块和图形都可以正确呈现,但不会相互影响。
非常感谢!
感谢@YBS,coeff()解决了