I am having trouble adding data labels to points in a reactive scatterplot. I cannot find any information online or in previous questions. Either the length is incorrect or I am referencing the wrong object.
df <- structure(list(Date = structure(c(19949, 19949, 19949, 19949,
19956, 19956, 19962, 19962, 19977, 19977, 19977, 19977, 19985,
19985, 19991, 19991, 19991, 19991, 19997, 19997), class = "Date"),
Unit = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L), levels = c("A",
"B"), class = "factor"), Species = structure(c(1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), levels = c("Dunlin", "Willet"), class = "factor"),
Tally = c(0L, 8L, 0L, 44L, 0L, 15L, 0L, 27L, 0L, 6L, 0L,
10L, 6L, 16L, 0L, 3L, 0L, 14L, 0L, 18L)), row.names = c(NA,
20L), class = "data.frame")
# Define ui
ui <- fluidPage(
dateRangeInput(inputId = "Date", label = "Date Range",start = min(df$Date),end=max(df$Date),min = min(df$Date),max=max(df$Date)),
selectInput(inputId = "Species",
label = "Choose Species",
choices = unique(df$Species),
selected = 'Dunlin'),
checkboxGroupInput(inputId = "Unit",
label = "Choose Units",
choices = unique(df$Unit),
selected = 'B'),
plotOutput("line"))
# Define server
server <- function(input, output) {
filtered_data <- reactive({
df %>%
filter(Species == input$Species, Unit %in% input$Unit, between(Date ,input$Date[1], input$Date[2]))})
output$line <- renderPlot({
ggplot(filtered_data(), aes(x = Date, y = Tally, color = Unit, group = Unit)) +
geom_line() +
geom_point() +
geom_text(label=input$Tally, check_overlap = T) +
scale_x_date(date_breaks = "2 weeks" , date_labels = "%d-%b")
})
}
shinyApp(ui = ui, server = server)
I am having trouble adding data labels to points in a reactive scatterplot. I cannot find any information online or in previous questions. Either the length is incorrect or I am referencing the wrong object.
df <- structure(list(Date = structure(c(19949, 19949, 19949, 19949,
19956, 19956, 19962, 19962, 19977, 19977, 19977, 19977, 19985,
19985, 19991, 19991, 19991, 19991, 19997, 19997), class = "Date"),
Unit = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L), levels = c("A",
"B"), class = "factor"), Species = structure(c(1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L), levels = c("Dunlin", "Willet"), class = "factor"),
Tally = c(0L, 8L, 0L, 44L, 0L, 15L, 0L, 27L, 0L, 6L, 0L,
10L, 6L, 16L, 0L, 3L, 0L, 14L, 0L, 18L)), row.names = c(NA,
20L), class = "data.frame")
# Define ui
ui <- fluidPage(
dateRangeInput(inputId = "Date", label = "Date Range",start = min(df$Date),end=max(df$Date),min = min(df$Date),max=max(df$Date)),
selectInput(inputId = "Species",
label = "Choose Species",
choices = unique(df$Species),
selected = 'Dunlin'),
checkboxGroupInput(inputId = "Unit",
label = "Choose Units",
choices = unique(df$Unit),
selected = 'B'),
plotOutput("line"))
# Define server
server <- function(input, output) {
filtered_data <- reactive({
df %>%
filter(Species == input$Species, Unit %in% input$Unit, between(Date ,input$Date[1], input$Date[2]))})
output$line <- renderPlot({
ggplot(filtered_data(), aes(x = Date, y = Tally, color = Unit, group = Unit)) +
geom_line() +
geom_point() +
geom_text(label=input$Tally, check_overlap = T) +
scale_x_date(date_breaks = "2 weeks" , date_labels = "%d-%b")
})
}
shinyApp(ui = ui, server = server)
Share
Improve this question
edited Mar 20 at 20:17
Jan
9,9256 gold badges20 silver badges33 bronze badges
asked Mar 20 at 14:28
Kyp CordtsKyp Cordts
1
1
|
1 Answer
Reset to default 0You are currently trying to use input$Tally
but that object doesn't exist - input$
values are created by *Input
functions like the selectInput
but you do not have an *Input
function with an id of Tally
.
You are creating a subset of your data as filtered_data
and this is the object that you should use to add labels to the plot. If you switch to using geom_text(label=filtered_data()$Tally, check_overlap = T)
it will work, but as Stefan points out geom_text(aes(label=Tally), check_overlap = T)
is the tidier way.
Tally
column usegeom_text(aes(label = Tally), ...)
. If that isn't your desired result please clarify what you are trying to achieve. – stefan Commented Mar 20 at 14:53