Hello,
This is part 2 of an earlier question that I had posted (part 1: Facet chart in a Shiny web app with lots of plots)
I have a shiny app that creates plots based on some grouping criteria. I want the individual facets/plots to be sorted by size. In the case of the gapminder example, by gpdPercap. I know that there are several posts about this (example: Ordering categories within ggplot2 facets | R-bloggers), but I am having a really hard time applying it to my time series example.
The current code and facet chart is shown below. Any help would be much appreciated!!
library(shiny)
library(tidyverse)
library(gapminder)
### Pre-process Data
## functions
pct_change <- function(x) {
((x - lag(x,1)) / lag (x,1)) * 100
}
## data frame to be used in shiny app
df <- gapminder %>%
select(country, year, gdpPercap) %>%
group_by(country) %>%
mutate(gdpPercap_change = pct_change(gdpPercap)) %>%
mutate(gdpPercap_change = round(gdpPercap_change, 2)) %>%
mutate(growth_group = case_when(year == 2007 & gdpPercap_change > 20 ~ "high growth",
year == 2007 & gdpPercap_change < 20 & gdpPercap_change > 0 ~ "moderate growth",
year == 2007 & gdpPercap_change < 0 ~ "declining",
TRUE ~ as.character(NA)
)) %>%
fill(growth_group, .direction = "up") %>%
na.omit()
head(df)
### UI portion
ui <- fluidPage(
titlePanel("Gapminder: Dashboard"),
sidebarLayout(
sidebarPanel(
selectInput(inputId="group", label="Choose a Growth Group",
choices=unique(df$growth_group), selected="declining")
),
mainPanel(
plotOutput("chart_1")
)
)
)
### Server portion
server <- function(input, output) {
output$chart_1 <- renderPlot({
chart <- df %>%
filter(growth_group == input$group) %>%
ungroup() %>%
ggplot(aes(x=year, y=gdpPercap_change)) +
geom_line() +
geom_hline(yintercept = 0) +
facet_wrap(~ country, scales = "free_y", ncol = 5)
chart
})
}
### run shiny app
shinyApp(ui = ui, server = server)