I have four datasets that I have shaped and combined into combined_data
. I’m able to get my shiny app to work with this dataset, showing data from each separate dataframe as a different coloured line.
The other dataset that I am attempting to include separately has the same input values as combined_data
, but a different y-axis scale. I’ve tried to add it with plotly
, but am getting the error [Object object].
I wanted to be able to see the data from eq08_total_employed
on my shiny app as well, but got the error Object:object when the app popped up.
Sample data:
combined_data <- data.frame(
Occupation = c("Teacher", "Teacher", "Teacher", "Doctor", "Doctor", "Doctor"),
quarter = c("Q2022q1", "Q2022q2", "Q2022q3", "Q2022q1", "Q2022q2", "Q2022q3"),
occupation_per_1000 = c(5.1, 5.3, 5.2, 8.5, 8.6, 8.7)
)
EQ08_employed_total <- data.frame(
Occupation = c("Teacher", "Teacher", "Teacher", "Doctor", "Doctor", "Doctor"),
quarter = c("Q2022q1", "Q2022q2", "Q2022q3", "Q2022q1", "Q2022q2", "Q2022q3"),
total_employed = c(1200, 1250, 1300, 1600, 1650, 1700)
)
ui <- fluidPage(
titlePanel("Occupation Data Visualization"),
sidebarLayout(
sidebarPanel(
selectInput("occupation", "Select Occupation:", choices = unique(combined_data$Occupation))
),
mainPanel(
plotlyOutput("linePlot")
)
)
)
server <- function(input, output) {
output$linePlot <- renderPlotly({
# Filter the datasets based on the selected occupation
combined_filtered <- combined_data %>%`
filter(Occupation == input$occupation)
eq08_employed_total <- EQ08_employed_total %>%
filter(Occupation == input$occupation)
# Create the Plotly object with two y-axes
p <- plot_ly() %>%
add_trace(
data = combined_filtered,
x = ~quarter,
y = ~occupation_per_1000,
type = 'scatter',
mode = 'lines+markers',
name = 'Occupation per 1000',
line = list(color = 'blue')
) %>%
add_trace(
data = eq08_employed_total,
x = ~quarter,
y = ~total_employed,
type = 'scatter',
mode = 'lines+markers',
name = 'Total Employed',
yaxis = 'y2',
line = list(color = 'green')
) %>%
layout(
title = paste("Occupation Data for", input$occupation),
xaxis = list(title = "Quarter"),
yaxis = list(
title = "Occupation per 1000",
autorange = TRUE
),
yaxis2 = list(
title = "Total Employed",
overlaying = "y",
side = "right"
),
margin = list(t = 60, b = 40, l = 60, r = 60)
)
p
})
}
# Run the application
shinyApp(ui = ui, server = server)
1