I'm trying to make an interactive graph in Shiny where you can use a slider to change a scaling value for one of the lines on a dual Y axis graph. Here is what the base graph looks like
The code I am using to plot the graph in Shiny is below, my aim was to define the "coeff" variable as the input of a slider in the UI, and to then incorporate this into how the values of the line were calculated
###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)
I'm not actually getting any error messages from this code; the slider and the graph both render properly but just don't interact with each other.
Many thanks!
CodePudding user response:
Thanks to @YBS, coeff() solved it