alignment of ggplots in a modularized shiny app - r

I have a modularized shiny app that displays in real time 4 variables. The monitor module takes as an input one big data frame, and displays the signal assigned. The 4 modules are stacked together in the ui:
tabBox(id = "monitoring_tabBox",
height = monitor_height_px,
width = "500px",
tabPanel(id = "layout1",
title = "Layout 1",
monitorModuleUI("sbto2"),
monitorModuleUI("icp"),
monitorModuleUI("map"),
monitorModuleUI("ptio2")
)
),
The problem is the following: the plots are not perfectly aligned across modules. Mainly because the y ticks values have different ranges (see how the icp and the ptio2 are aligned because both signals have two digits, without decimals)
I've seen several techniques to align ggplots but you need to take as an input the 4 plots, and then merge them all in a single plot / render. I would like to avoid this step to keep the modularized structure of the app.
Is there any way I can align those plots without having to marge them together? (i.e by constraining the length of the y ticks)
Thank you in advance !
Screenshot:
screenshot showing the 4 modules and the misalignment issue
Reproducible example:
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
# Sample Data
df <- data.frame(timestamp = seq(as.POSIXct("2020-06-01 10:00:00"), as.POSIXct("2020-06-01 12:00:00"), "1 min"),
sbto2 = round(10000*rnorm(121, 0, 2), 1),
map = round(100*rnorm(121, 0, 2), 1),
icp = round(10*rnorm(121, 0, 1.5), 1),
ptio2 = round(1000*rnorm(121, 0, 1.2), 1))
# monitorModule
monitorModuleUI <- function(id){
ns <- NS(id)
fluidRow(
column(8,
plotOutput(ns("monitoring_plot"),
height = "150px")
),
column(2,
uiOutput(ns("monitoring_text"))
)
)
}
monitorModule <- function(input, output, server, variable_name, df){
output$monitoring_plot <- renderPlot({
p()
})
p <- reactive({
df %>%
gather("var", "value",seq(2,5)) %>%
filter(var == variable_name) %>%
ggplot(aes(x = timestamp, y = value)) + geom_line() -> p
return(p)
})
output$monitoring_text <- renderUI({
value <- p()$data$value[nrow(p()$data)]
h1(strong(paste(value)), style = "font-size:90;")
})
}
# APP
ui <- shinyServer(fluidPage(
monitorModuleUI("sbto2"),
monitorModuleUI("icp"),
monitorModuleUI("ptio2"),
monitorModuleUI("map")
))
server <- shinyServer(function(input, output, session){
callModule(monitorModule, "sbto2", "sbto2", df)
callModule(monitorModule, "icp", "icp", df)
callModule(monitorModule, "ptio2", "ptio2", df)
callModule(monitorModule, "map", "map", df)
})
shinyApp(ui=ui,server=server)

One alternative would be to return a reactive plot from each module and then to organize them with the package {patchwork}.
Here's an example:
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
library(patchwork)
# Sample Data
df <- data.frame(timestamp = seq(as.POSIXct("2020-06-01 10:00:00"), as.POSIXct("2020-06-01 12:00:00"), "1 min"),
sbto2 = round(10000*rnorm(121, 0, 2), 1),
map = round(100*rnorm(121, 0, 2), 1),
icp = round(10*rnorm(121, 0, 1.5), 1),
ptio2 = round(1000*rnorm(121, 0, 1.2), 1))
# monitorModule
monitorModuleUI <- function(id){
# ns <- NS(id)
# plotOutput(ns("monitoring_plot"),
# height = "150px")
}
monitorModule <- function(input, output, server, variable_name, df){
test <- reactive({
df %>%
gather("var", "value",seq(2,5)) %>%
filter(var == variable_name) %>%
ggplot(aes(x = timestamp, y = value)) + geom_line() -> p
return(p)
})
}
# APP
ui <- fluidPage(
monitorModuleUI("sbto2"),
monitorModuleUI("icp"),
monitorModuleUI("ptio2"),
monitorModuleUI("map"),
plotOutput("all_plots")
)
server <- function(input, output, session){
plot_1 <- callModule(monitorModule, "sbto2", "sbto2", df)
plot_2 <- callModule(monitorModule, "icp", "icp", df)
plot_3 <- callModule(monitorModule, "ptio2", "ptio2", df)
plot_4 <- callModule(monitorModule, "map", "map", df)
output$all_plots <- renderPlot({
plot_1() / plot_2() / plot_3() / plot_4()
})
}
shinyApp(ui=ui,server=server)

Related

In Shiny/R how to control number of Ggplots charts displayed with SliderInput

I need to control the number of chart using a slideInput.
I have a list with ggplot charts. Once my slider came from 1 to 2 it will display the first 2 charts of this list. If the slider range is from 1:3 it will display the first 3 charts from this list chart.
This is what Ive done so far:
library(shiny)
library(gapminder)
library(highcharter)
df <- gapminder %>% group_split(country)
countries <- df[1:10] %>% set_names(1:10)
ggplots_list <- countries %>% map(~ .x %>% ggplot(aes(x = year, y = pop)) + geom_line())
library(shiny)
ui <- fluidPage(
sliderInput(inputId = "slider_new",
label = "Projections Range",
width = '100%',
min = 1, max = 10,
value = 1
) ,
plotOutput('chart_1', height = '500px')
)
server <- function(input, output, session) {
output$chart_1 <- renderPlot({
ggplots_list[input$slider_new[1]]
})
}
shinyApp(ui, server)
The idea is to have a grid of charts as I increase the slider value.
Any help?
Try this
library(gapminder)
library(highcharter)
library(purrr)
df <- gapminder %>% group_split(country)
countries <- df[1:10] %>% set_names(1:10)
ggplots_list <- countries %>% map(~ .x %>% ggplot(aes(x = year, y = pop)) + geom_line())
library(shiny)
ui <- fluidPage(
sliderInput(inputId = "slider_new",
label = "Projections Range",
width = '100%',
min = 1, max = 10,
value = 1
) ,
uiOutput("chart_1")
)
server <- function(input, output, session) {
lapply(1:10, function(i){
output[[paste0("plots",i)]] <- renderPlot({ ggplots_list[i] })
})
output$chart_1 <- renderUI({
n <- input$slider_new
lapply(1:n, function(i) plotOutput(paste0("plots",i), height=500))
})
}
shinyApp(ui, server)

create and showing ranking with bar chart in R shiny

I'm new in programming language especially R.
I have data frame and want to show top 3 of my data and sort from the biggest value using bar chart. I have tried some codes but failed to create proper chart. Here is my latest code :
library(shiny)
library(plotly)
my_data <- data.frame(x1 = c("a","b", "c","d","e","f","g","h"),
x2 = c(200, 200, 100,200,200,100,200,100),
x3 = c(100,400,500,50,100,300,100,50))
df1 <- my_data[order(my_data$x3),] #order by x3 value, to create rank
ui <- tabPanel("Test",
sidebarLayout(
sidebarPanel(
selectInput(inputId = "why",
label = "1. Select",
choices = df1$x2),
),
mainPanel(plotlyOutput("test"))
))
server <- function(input, output, session) {
output$test <- renderPlotly({
df2 <- df1 %>%
filter(x2 ==input$why) #filter by x2
p <-ggplot(df2,
aes(x = x1, y=x3)) +
geom_bar(stat = "identity")
fig <- ggplotly(p)
fig
})}
shinyApp(ui = ui, server = server)
the bar chart I created was not ordered correctly (based on x3 values), and I also only want to show top 3 of my data
To filter for the top 3 rows you could use dplyr::slice_max and to reorder your bars use e.g. reorder. Simply reordering the dataset will not work.
library(shiny)
library(dplyr)
library(plotly)
ui <- tabPanel(
"Test",
sidebarLayout(
sidebarPanel(
selectInput(
inputId = "why",
label = "1. Select",
choices = unique(df1$x2),
selected = 200
),
),
mainPanel(plotlyOutput("test"))
)
)
server <- function(input, output, session) {
output$test <- renderPlotly({
df2 <- df1 %>%
filter(x2 == input$why) %>%
slice_max(x3, n = 3, with_ties = FALSE)
p <- ggplot(
df2,
aes(x = reorder(x1, -x3), y = x3)
) +
geom_bar(stat = "identity")
fig <- ggplotly(p)
fig
})
}
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:8022
I know the question is already answered, but I encourage you to keep your server function as small as possible and try to wrap long series of code into functions.
Here is an example of what I mean
library(tidyverse)
library(shiny)
library(plotly)
my_data <- data.frame(x1 = c("a","b", "c","d","e","f","g","h"),
x2 = c(200, 200, 100,200,200,100,200,100),
x3 = c(100,400,500,50,100,300,100,50))
df1 <- my_data[order(my_data$x3),] #order by x3 value, to create rank
myPlot <- function(data, input) {
df <- data |>
filter(x2 == input) #filter by x2
p <-ggplot(df, aes(x = reorder(x1, -x3), y=x3)) +
geom_bar(stat = "identity")
return(ggplotly(p))
}
ui <- tabPanel("Test",
sidebarLayout(
sidebarPanel(
selectInput(inputId = "why",
label = "1. Select",
choices = df1$x2),
),
mainPanel(plotlyOutput("test"))
))
server <- function(input, output, session) {
output$test <- renderPlotly({
myPlot(df1, input$why)
})
}
shinyApp(ui = ui, server = server)

Shiny / Plotly: Update plot with labels of only selected points

Using R Shiny and plotly I created a interactive scatter plot.
How can I modify my code to interactively label only the points which were selected by the user?
Example plot
Thank you so much for your help!
All the best,
Christian
library(plotly)
library(shiny)
library(dplyr)
data <- data.frame(matrix(runif(500,0,1000), ncol = 2, nrow = 100)) %>%
mutate(ID = row_number())
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("hover"),
verbatimTextOutput("click"),
verbatimTextOutput("brush"),
verbatimTextOutput("zoom"))
server <- function(input, output, session) {
output$plot <- renderPlotly({
p <- ggplot(data, aes(x = X1, y = X2, key = ID)) +
geom_point()
ggplotly(p) %>% layout(dragmode = "select")
})
}
shinyApp(ui, server)
Below is a possible solution. I use a reactive function to "label" selected points. I wasn't sure how exactly you want to display the IDs for selected points. The code adds the ID as text when a point is selected. Also, I add some jitter to move the IDs away from the points.
library(plotly)
library(shiny)
library(dplyr)
data <- data.frame(matrix(runif(500,0,1000), ncol = 2, nrow = 100)) %>%
mutate(ID = row_number())
ui <- fluidPage(
plotlyOutput("plot"),
verbatimTextOutput("hover"),
verbatimTextOutput("click"),
verbatimTextOutput("brush"),
verbatimTextOutput("zoom"))
server <- function(input, output, session) {
output$plot <- renderPlotly({
data <- get_data()
p <- ggplot(data, aes(x = X1, y = X2, key = ID)) +
geom_point() + geom_text(data=subset(data, show_id),aes(X1,X2,label=ID), position = position_jitter(width = 20,height = 20))
ggplotly(p, source = "subset") %>% layout(dragmode = "select")
})
get_data <- reactive({
event.data <- event_data("plotly_selected", source = "subset")
data <- data %>% mutate(show_id = FALSE)
if (!is.null(event.data)) {
data$show_id[event.data$pointNumber + 1] <- TRUE
}
data
})
}
shinyApp(ui, server)

bars missing when using shiny to create ggplot bar chart

I used shiny and created a app.R file to hope to build a bar chart with ggplot. I also used checkboxGroupInput to create a 2 check boxes to control the condition. While the total number of bars should be 28 after all boxes are checked, but the maximum seemed to allow only 17 bars for some reason. So some bars (row of data) are missing. The missing bars don't seems to have a pattern. Can someone please help ?
dataset:https://drive.google.com/open?id=1fUQk_vMJWPwWnIMbXvyd5ro_HBk-DBfc
my code:
midterm <- read.csv('midterm-results.csv')
library(dplyr)
library(tidyr)
# get column number for response time
k <- c(33:88)
v <- c()
for (i in k){
if (i%%2 == 1){
v <- c(v,i)
}
}
#average response time by question
time <- midterm[ , v]
new.col.name <- gsub('_.*', "", colnames(time))
colnames(time) <- new.col.name
avg.time <- data.frame(apply(time, 2, mean))
avg.time$question <- rownames(avg.time)
colnames(avg.time) <- c('response_time', 'question')
rownames(avg.time) <- NULL
avg.time$question <- factor(avg.time$question,
levels = c('Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8.9',
'Q10','Q11','Q12.13','Q14','Q15','Q16','Q17',
'Q18','Q19','Q20','Q21','Q22','Q23','Q24','Q25',
'Q26','Q27','Q28','Q29','Q30'))
avg.time$question_type <- c(1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0)
# I did this manually because the there when data was imported into the midterm.csv,
# q8 & 9, q12 &13 were accidentally merged (28 v.s 30 question)
avg.time$question_type <- ifelse(avg.time$question_type == 1,
'googleable', 'not googleable')
avg.time$question_type <- factor(avg.time$question_type,
levels = c('googleable', 'not googleable'))
library(shiny)
library(ggplot2)
ui <- fluidPage(
checkboxGroupInput(inputId = "type",
label = "select question type",
choices = levels(avg.time$question_type),
selected = TRUE),
plotOutput('bar')
)
server <- function(input, output) {
output$bar <- renderPlot({
ggplot(avg.time[avg.time$question_type==input$type, ],
aes(x=question, response_time)) +
geom_bar(aes(fill = question_type), stat='identity', width = 0.5)
}, height =500, width = 1000)
}
shinyApp(ui = ui, server = server)
library(shiny)
library(ggplot2)
ui <- fluidPage(
checkboxGroupInput(inputId = "type", label = "select question type",
choices = levels(avg.time$question_type), selected = TRUE),
plotOutput('bar')
)
server <- function(input, output) {
data <- reactive(avg.time[avg.time$question_type %in% input$type, ])
output$bar <- renderPlot({
ggplot(data(),
aes(x=question, response_time)) + geom_bar(stat='identity', width = 0.5,
aes(fill = question_type))
}, height =500, width = 1000)
}
shinyApp(ui = ui, server = server)
of course you can use avg.time[avg.time$question_type %in% input$type, ] inside ggplot2 but reactivity is better.

Change labels in R shiny/ggvis plot

I need to have with ggvis in shiny the labels= axis feature.
In R the labels axis argument allow to change the x labels (adding a Kb format), that keep the order and the relative distance between items:
mtcars <- mtcars[1:10, ]
my_data <- mtcars[order(mtcars$disp),]
xpos <- sort(mtcars$disp)
plot(my_data$disp, my_data$mpg, type = "l", xaxt="n")
axis(1, at=xpos, labels=sprintf("%.2fKb", xpos/10))
With this we get what we need:
Now we try to get the exact same on shiny with ggvis:
library(shiny)
library(ggvis)
mtcars
ui <- pageWithSidebar( div(),
sidebarPanel(uiOutput("plot_ui"),width=2),
mainPanel(ggvisOutput("plot"))
)
server <- function(input, output, session) {
mtc <- reactive({
my_data <- mtcars[1:10, ]
# Do some sorting/ordering if you want (here sorted by disp)
my_data <- my_data[order(my_data$disp), ]
my_labels <- c(as.character(paste0((my_data$disp)/1000, "Kb")))
y <- my_data$mpg
x <- factor(c(my_data$disp), labels = c(unique(my_labels)))
data.frame(x = x, y = y)
})
mtc %>%
ggvis(~x,~y ) %>%
layer_lines() %>%
add_axis("x", properties=axis_props(labels=list(fontSize = 10))) %>%
bind_shiny("plot", "plot_ui")
}
shinyApp(ui = ui, server = server)
As we highlight in red, the distances are not correct. So how can we have on shiny the same plot we have on plain R with axis(label=... ?
Ok, I fiddled around with it and you have to convert your x-axis into a factor first. I've added some sorting to the dataframe as you probably want these to be in order also (you can easily remove it otherwise). You can put labels on the axis yourself. Let me know if you have any questions. Also I changed the data on the x-axis to display mtcars$disp as it has values in 100s
rm(list = ls())
library(shiny)
library(ggvis)
mtcars
ui <- pageWithSidebar(
div(),
sidebarPanel(
sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),value = 10, step = 1),
uiOutput("plot_ui"),width=2),
mainPanel(ggvisOutput("plot"))
)
server <- function(input, output, session) {
mtc <- reactive({
my_data <- mtcars[1:input$n, ]
# Do some sorting/ordering if you want (here sorted by disp)
my_data <- my_data[order(my_data$disp),]
my_labels <- c(as.character(paste0((my_data$disp)/1000,"Kb")))
y <- my_data$mpg
x <- factor(c(my_data$disp), labels=c(unique(my_labels)))
data.frame(x = x, y = y)
})
mtc %>%
ggvis(~x,~y ) %>%
layer_lines() %>%
add_axis("x", properties=axis_props(labels=list(fontSize = 10))) %>%
bind_shiny("plot", "plot_ui")
}
shinyApp(ui = ui, server = server)
Updated Output

Resources