I have this dataframe which contains the Value of each Country per VaccinationWeek and the cumulative Value (EU) for every week.
VaccinationWeek Country Value
1 2020w1 EU 3
2 2020w1 CHE 2
3 2020w1 ITA 1
4 2020w2 EU 5
5 2020w2 CHE 3
6 2020w2 ITA 2
and I have created a shiny app in which when the user have selected (default) no Countries from pickerInput() then only the EU line will be displayed in the line chart like:
For every country then that the user selects (normally more than 2) in the line described above will be added the respective line for this country and the hover text will include data for both EU and the selected Country in all lines like:
library(shiny)
library(plotly)
library(shinyWidgets)
VaccinationWeek<-c("2020w1","2020w1","2020w1","2020w2","2020w2","2020w2")
Country<-c("EU","CHE","ITA","EU","CHE","ITA")
Value<-c(3,2,1,5,3,2)
dat<-data.frame(VaccinationWeek,Country,Value)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
pickerInput(
inputId = "p1",
label = "",
choices = c("CHE","ITA"),
multiple = TRUE,
selected = c("CHE","ITA"),
options = pickerOptions(`actions-box` = TRUE)
)
),
mainPanel(
plotlyOutput("line")
)
)
)
server <- function(input, output) {
output$line<-renderPlotly({
dat <- subset(dat, Country %in% input$p1)
plot_ly(dat,
x = ~VaccinationWeek,
y = ~Value,
text = ~Value) %>%
add_trace(
type = 'scatter',
mode = 'lines+markers+text',
fill = 'tozeroy',
fillcolor = 'lightgreen',
marker = list(color = 'green'),
line = list(color = 'green'),
textposition = "top center",
hovertemplate = paste0("<b>%{x}</b>
Value: %{y}
<extra></extra>"),
hoveron = 'points')
})
}
shinyApp(ui = ui, server = server)



