Could you help me adjust the legend of my graph?. I will insert an image showing how I would like it to look. I adjusted the insert
of legend
, however when I go to run the shiny, it doesn't show. I would like the legend to be under the graph.
Code below:
library(shiny)
library(shinythemes)
df1 <- structure(
list(date1= c("2021-06-28","2021-06-28","2021-06-28","2021-06-28"),
date2 = c("2021-06-30","2021-06-30","2021-07-01","2021-07-02"),
Category = c("FDE","ABC","FDE","ABC"),
Week= c("Wednesday","Wednesday","Friday","Friday"),
DR1 = c(4,1,6,1),
DR01 = c(4,1,4,4), DR02= c(4,2,6,0),DR03= c(9,5,4,0),
DR04 = c(5,4,3,5),DR05 = c(5,4,5,0),
DR06 = c(2,4,3,5),DR07 = c(2,5,4,0),
DR08 = c(3,4,5,0),DR09 = c(2,3,4,0)),
class = "data.frame", row.names = c(NA, -4L))
ui <- fluidPage(
shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput('date'),
uiOutput('mycode')
),
mainPanel(
tabsetPanel(
tabPanel("", plotOutput("graph",width = "100%", height = "600")
)
))
))))
server <- function(input, output,session) {
data<-reactive(df1)
output$date <- renderUI({
req(data())
all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
disabled <- as.Date(setdiff(all_dates, as.Date(data()$date2)), origin = "1970-01-01")
entrydate<- dateInput(input = "Date2",
label = h4("Choose"),
min = min(data()$date2),
max = max(data()$date2),
format = "dd-mm-yyyy",
datesdisabled = disabled)
entrydate$children[[2]]$attribs$placeholder <- "No selected date"
entrydate
})
output$mycode <- renderUI({
req(input$Date2)
df1 <- data()
df5 <- df1[as.Date(df1$date2) %in% input$Date2,]
selectInput("code", label = h4("Category"),choices=c("No code selected" = "", sort(unique(df5$Category))))
})
output$graph <- renderPlot({
df1 <- data()
req(input$Date2,input$code)
plot(x=df1$DR02,y=df1$DR1)
legend("bottomright",inset=c(-0,-0), legend= 3,title="The number is é:",title.col = "black", cex = 1.2)})
}
shinyApp(ui = ui, server = server)
Example: