-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_bar_plot.R
78 lines (57 loc) · 2.51 KB
/
add_bar_plot.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
###############################################
### Bar-plot Module ###
###############################################
# Creates a bar-plot with the chosen stations, parameters and time range
###################################################################
### Output Module ####
#################################################################
barplot_output <- function(id) {
ns <- NS(id)
plotOutput(ns("barplot_plot"))
}
######################################################################
# Server Module ####
######################################################################
barplot_server <- function(id,
data_measurements,
parameter,
overview_component,
theme_plots) {
moduleServer(id, function(input, output, session) {
ns <- session$ns
output$barplot_plot <- renderPlot({
# Get the data to plot
data_plot <- data_measurements()
# Check if there is data to plot
shiny::validate(
need(!is_empty(data_plot) | !dim(data_plot)[1] == 0,
i18n$t("expl_no_sensor_data"))
)
# Determine parameter for the label in the plot
parameter <- parameter()
# Find the corresponding label
parameter_label <- overview_component %>%
dplyr::filter(component == parameter) %>%
dplyr::pull(label)
# Calculate the mean and standard deviation
data_barplot <- data_plot %>%
dplyr::group_by(parameter, label) %>%
dplyr::mutate(gemiddelde = round(mean(value, na.rm=TRUE), 2),
standaarddev = sd(value, na.rm = T),
n_obs = n()) %>%
distinct(label, .keep_all = TRUE)
# Make a plot
try(ggplot(data = data_barplot, aes(y=gemiddelde, x=label)) +
geom_bar(stat="identity", fill=paste0(data_barplot$col), color = 'black',
size = data_barplot$size/2) +
labs(x = element_blank(), y = expression(paste("Concentration (", mu, "g/",m^3,")")),
title=paste0('Barplot for: ', parameter_label,
" ", min(data_plot$date) %>% format("%d/%b/%Y"),
" - ", max(data_plot$date) %>% format("%d/%b/%Y")
)) +
expand_limits(y=0) + # Make sure no negative values are shown
theme_plots
)
})
})
}