I'm having two problems and maybe they might be separate questions. But since shiny
has a set of particularities, I've decided to state both problems here.
(I) The first problem is related to using sliderInput function with facet_grid
. For some reason, the subsetting does not seem to be working within a range but just picking the extreme values of the slider bar.
(I want the plot to show the range and not the values picked in each extreme)
(II) The second problem is related to the ordering of the Company_Name
variable in my sample data (sample_data
). I cannot apply a ordering within groups of the Year
, so that my bar graph is ordered by each year.
(I want the values ordered by each year)
Here it is the code:
library(shiny)
library(ggplot2)
library(dplyr)
sample_data = data.frame(Company_Name=c("Company 1","Company 2","Company 3",
"Company 1","Company 2","Company 3",
"Company 1","Company 2","Company 3"),
Profits_MM = c(20,100,80,
45,120,70,
50,110,130),
Sales_MM = c(200,800,520,
300,1000,630,
410,1150,1200),
Year=c(2016,2016,2016,
2017,2017,2017,
2018,2018,2018))
# UI
ui <- fluidPage(
sidebarLayout(
# Input(s)
sidebarPanel(
checkboxGroupInput(inputId = "sel_com",
label = "Company Selection:",
choices = c("Company 1","Company 2","Company 3"),
selected = "Company 1"),
selectInput(inputId = "y",
label = "Performance Variable",
choices = c("Profits (in Millions)" = "Profits_MM",
"Sales (in Millions)" = "Sales_MM"),
selected = "Profits_MM"),
sliderInput("year","Year Selection:",
min=2016,
max=2018,
value=c(2017,2018),
step=1)
),
# Output(s)
mainPanel(
plotOutput(outputId = "barplot")
)
)
)
# Server
server <- function(input, output, session) {
companies_sel <- reactive({
req(input$sel_com)
sample_data_gg = filter(sample_data, Company_Name %in% input$sel_com)
# print(sample_data_gg)
sample_data_gg
})
year_sample <- reactive({
req(input$year)
sample_data_gg = sample_data
if(length(input$year)>1){
Years = seq(input$year[1],input$year[2])
sample_data_gg = filter(companies_sel(), Year %in% Years)
}
if(length(input$year==1)){
sample_data_gg = filter(companies_sel(), Year %in% input$year)
}
# print(sample_data_gg)
sample_data_gg
})
output$barplot = renderPlot({
sample_data_gg = year_sample()
sample_data_gg = sample_data_gg %>%
group_by(Year) %>%
mutate(Sigla = factor(Company_Name,
levels = Company_Name[order(input$y)]))
y <- input$y
ggplot(data = sample_data_gg, aes(x=Company_Name, y =get( y ))) +
geom_col(position="dodge", fill="darkred") +
facet_grid(Year~., scales = "free_y") +
theme(axis.text.x = element_text(angle = 60, hjust = 1))
})
}
shinyApp(ui = ui, server = server)
This question is related to this one here
Edit: Using the functions posted as answers in this question get the job done for problem II:
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
new_x <- paste(x, within, sep = sep)
stats::reorder(new_x, by, FUN = fun)
}
scale_x_reordered <- function(..., sep = "___") {
reg <- paste0(sep, ".+$")
ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
And changing the the rendered plot to:
sample_data_gg = year_sample()
y <- input$y
ggplot(data = sample_data_gg, aes(x=reorder_within(Company_Name, get( y ), Year), y = get( y ))) +
geom_col(position="dodge", fill="darkred") +
facet_wrap(Year~., scales = "free") +
scale_x_reordered() +
theme(axis.text.x = element_text(angle = 60, hjust = 1))