Modérateur : Groupe des modérateurs
Code : Tout sélectionner
result_city <- c("rezé", "nantes", "la-baule", "blain", "vallet", "nantes", "la-baule", "blain", "vallet", "rezé")
result_city <- factor(result_city)
Code : Tout sélectionner
result_city <- factor(c("rezé", "nantes", "la-baule", "blain", "vallet", "nantes", "la-baule", "blain", "vallet", "rezé"))
Code : Tout sélectionner
dpe <- data.frame(
annee_construction_tranche = factor(c(
"2011 et années suivantes", "1991-2010", "1921-1970", "1991-2010",
"Avant 1870", "1870-1920", "1921-1970", "1971-1990", "1991-2010",
"2011 et années suivantes"
)),
result_city = factor(c(
"rezé", "nantes", "la-baule", "blain", "vallet",
"nantes", "la-baule", "blain", "vallet", "rezé"
)),
consommation_energien_revue = c(10, 210, 230, 190, 350, 170, 390, 430, 270, 330),
cut = c(2, 11, 12, 10, 18, 9, 20, 22, 14, 17)
)
Code : Tout sélectionner
dpe <- data.frame(
annee_construction_tranche = factor(c(
"2011 et années suivantes", "1991-2010", "1921-1970", "1991-2010",
"Avant 1870", "1870-1920", "1921-1970", "1971-1990", "1991-2010",
"2011 et années suivantes"
)),
result_city = factor(c(
"rezé", "nantes", "la-baule", "blain", "vallet",
"nantes", "la-baule", "blain", "vallet", "rezé"
)),
consommation_energien_revue = c(10, 210, 230, 190, 350, 170, 390, 430, 270, 330),
cut = c(2, 11, 12, 10, 18, 9, 20, 22, 14, 17)
)
donnee <- function() { # to mimic shiny app
dpe %>%
filter(annee_construction_tranche == "1991-2010") %>%
filter(result_city == "blain")
}
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(ggplot2)
format_data <- function(data) { # because "idem" is not a thing in programming, it's a function
data %>%
add_count(nb_total = n()) %>%
group_by(cut) %>%
summarise(nb_cut = n(), PCT = round(n() / nb_total * 100, 2), .groups = "drop") %>%
mutate(
cut_valeur = cut(x = cut, breaks = c(0:28), labels = seq(30, 570, 20)), # cut(), not case_when to cut a vector into pieces
color = cut(x = cut, breaks = c(0, 2, 4, 7, 11, 16, 22, 30), labels = LETTERS[1:7])
)
}
combi <- left_join(
x = format_data(dpe),
y = format_data(donnee()),
by = "cut",
suffix = c("_g", "_f")
)
cols <- c(
"A" = "#008000",
"B" = "#00FF00",
"C" = "#BBF616",
"D" = "#FFFF00",
"E" = "#FF8000",
"F" = "#F65200",
"G" = "#FF0000"
)
g <- ggplot(combi) +
geom_col(aes(x = cut_valeur_f, y = PCT_f, fill = color_f)) +
geom_point(aes(x = cut_valeur_g, y = PCT_g), shape = 4, size = 1) +
theme(
panel.background = element_rect(fill = "white", colour = "white", size = 2, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = "solid", colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = "solid", colour = "white"),
legend.title = element_blank()
) +
scale_fill_manual(values = cols) +
scale_x_discrete(drop = FALSE) + # drop to print all levels of a factor including the missing ones
labs(x = "Consommation énergétique moyenne en kWh par m²", y = "% des DPE")
g
#> Warning: Removed 9 rows containing missing values (position_stack).
Code : Tout sélectionner
library(shiny)
library(readr)
library(dplyr)
library(shinythemes)
library(DT)
library(plotly)
library(tidyverse)
annee_construction_tranche <- c("2011 et années suivantes", "1991-2010", "1921-1970", "1991-2010", "Avant 1870", "1870-1920","1921-1970","1971-1990","1991-2010","2011 et années suivantes")
annee_construction_tranche <- factor(annee_construction_tranche)
result_city <- c("rezé", "nantes", "la-baule", "blain", "vallet", "nantes", "la-baule", "blain", "vallet", "rezé")
result_city <- factor(result_city)
consommation_energien_revue <- c(10,210,230,190,350,170,390,430,270,330)
cut <- c(2,11,12,10,18,9,20,22,14,17)
dpe <- data.frame(annee_construction_tranche, result_city, consommation_energien_revue, cut)
ui <- fluidPage(
selectInput(inputId = "periode_choisie",
label = "La période de construction :",
choices = c("Toutes",levels(dpe$annee_construction_tranche)),
selected = "Toutes"),
selectInput(inputId = "commune",
label = "Votre commune :",
choices = c("Toutes",levels(dpe$result_city)),
selected = "Toutes"),
plotlyOutput("graph")
)
server <- function(input, output){
donnee <- reactive({
if (input$periode_choisie == "Toutes" & input$commune == "Toutes") {
df <- dpe
} else {
df <- dpe %>%
filter(annee_construction_tranche == input$periode_choisie) %>%
filter(result_city == input$commune)}
return(df)
})
output$graph <- renderPlotly ({
#somme du nombre de relevés de conso d'énergie
nb<- donnee() %>% summarise(nb = n())
#nombre de relevés par classe / cut
Stat <- donnee() %>% group_by(cut) %>% summarise(nb_cut =n())
#pourcentage de relevés par classe
Stat$PCT <- round(Stat$nb_cut / as.integer(nb) * 100, 2)
#création d'une colonne label/valeur en fonction du cut :
Stat$cut_valeur <-as.factor( case_when(
(Stat$cut<=1) ~ 30,
(Stat$cut<=2) ~ 50,
(Stat$cut<=3) ~ 70,
(Stat$cut<=4) ~ 90,
(Stat$cut<=5) ~ 110,
(Stat$cut<=6) ~ 130,
(Stat$cut<=7) ~ 150,
(Stat$cut<=8) ~ 170,
(Stat$cut<=9) ~ 190,
(Stat$cut<=10) ~ 210,
(Stat$cut<=11) ~ 230,
(Stat$cut<=12) ~ 250,
(Stat$cut<=13) ~ 270,
(Stat$cut<=14) ~ 290,
(Stat$cut<=15) ~ 310,
(Stat$cut<=16) ~ 330,
(Stat$cut<=17) ~ 350,
(Stat$cut<=18) ~ 370,
(Stat$cut<=19) ~ 390,
(Stat$cut<=20) ~ 410,
(Stat$cut<=21) ~ 430,
(Stat$cut<=22) ~ 450,
(Stat$cut<=23) ~ 470,
(Stat$cut<=24) ~ 490,
(Stat$cut<=25) ~ 510,
(Stat$cut<=26) ~ 530,
(Stat$cut<=27) ~ 550,
(Stat$cut<=28) ~ 570
))
#création de la colonne classe (color) en fonction du cut :
Stat$color<-as.factor( case_when(
(Stat$cut<=2) ~ "A",
(Stat$cut<=4) ~ "B",
(Stat$cut<=7) ~ "C",
(Stat$cut<=11) ~ "D",
(Stat$cut<=16) ~ "E",
(Stat$cut<=22) ~ "F",
(Stat$cut<=30) ~ "G"
))
#idem pour les données globales à comparer
nb_g <- dpe %>% summarise(nb = n())
Stat_g <- dpe %>% group_by(cut) %>% summarise(nb_cut =n())
Stat_g$PCT_g <- round(Stat_g$nb_cut / as.integer(nb_g) * 100, 2)
Stat_g$cut_valeur <-as.factor( case_when(
(Stat_g$cut<=1) ~ 30,
(Stat_g$cut<=2) ~ 50,
(Stat_g$cut<=3) ~ 70,
(Stat_g$cut<=4) ~ 90,
(Stat_g$cut<=5) ~ 110,
(Stat_g$cut<=6) ~ 130,
(Stat_g$cut<=7) ~ 150,
(Stat_g$cut<=8) ~ 170,
(Stat_g$cut<=9) ~ 190,
(Stat_g$cut<=10) ~ 210,
(Stat_g$cut<=11) ~ 230,
(Stat_g$cut<=12) ~ 250,
(Stat_g$cut<=13) ~ 270,
(Stat_g$cut<=14) ~ 290,
(Stat_g$cut<=15) ~ 310,
(Stat_g$cut<=16) ~ 330,
(Stat_g$cut<=17) ~ 350,
(Stat_g$cut<=18) ~ 370,
(Stat_g$cut<=19) ~ 390,
(Stat_g$cut<=20) ~ 410,
(Stat_g$cut<=21) ~ 430,
(Stat_g$cut<=22) ~ 450,
(Stat_g$cut<=23) ~ 470,
(Stat_g$cut<=24) ~ 490,
(Stat_g$cut<=25) ~ 510,
(Stat_g$cut<=26) ~ 530,
(Stat_g$cut<=27) ~ 550,
(Stat_g$cut<=28) ~ 570
))
Stat_g$color<-as.factor( case_when(
(Stat_g$cut<=2) ~ "A",
(Stat_g$cut<=4) ~ "B",
(Stat_g$cut<=7) ~ "C",
(Stat_g$cut<=11) ~ "D",
(Stat_g$cut<=16) ~ "E",
(Stat_g$cut<=22) ~ "F",
(Stat_g$cut<=30) ~ "G"
))
#fusion du global et du filtré
combi = left_join(Stat_g, Stat, by=c("cut"="cut"), suffix= c("_g","_f"))
cols <- c("A" = "#008000", "B" = "#00FF00", "C" = "#BBF616", "D" = "#FFFF00", "E" = "#FF8000", "F" = "#F65200", "G" = "#FF0000")
g <- ggplot(combi) +
geom_col(aes(x = cut_valeur_f, y = PCT, group=cut_valeur_f, fill = color_f), stat = "identity") +
geom_point(aes(x=cut_valeur_g, y=PCT_g, group=cut_valeur_g), shape = 4, size =1)+
theme(panel.background = element_rect(fill = "white", colour = "white",size = 2, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',colour = "white"),
legend.title = element_blank())+
ylab("% des DPE") +
scale_fill_manual(values=cols)+
scale_x_discrete("Consommation énergétique moyenne en kWh par m²", limits = c(30,50,70,90,110,130,150,170,190,210,230,250,270,290,310,330,350,370,390,410,430,450,470,490,510,530,550,570))
#breaks = c(0,50,150,250,350,450)
#pour enlever la possibilité d'enregistrer l'image, la zoomer, etc
ggplotly(g) %>% #, tooltip = "PCT"
config(displaylogo = FALSE, collaborate = FALSE,
displayModeBar = FALSE)
})
}
shinyApp(ui=ui, server=server)[/quote]
Retourner vers « Questions en cours »
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 1 invité