Problem with titles and subplot

I have a problem with this code.
When I draw just one plot, I have title and when I draw seven plots, I have no title to each plot.
Could you help me ?


> TU <- readRDS("TU.rds")
> View(TU)
> head(TU)
         IDFOURNEE NUMFOUR TOTALTAP VALIDFAB      DATEHEUREDEBUT        DATEHEUREFIN                     MATERIAUX TRAITEMENT FAMILLEPIECE1 FAMILLEPIECE2
1 F31-2017/01/02-2     F31      180        1 2017-01-02 13:53:00 2017-01-02 18:55:00                        Titane     Recuit        VIROLE              
2 F31-2017/01/02-3     F31      600        1 2017-01-02 20:19:00 2017-01-03 02:24:00 Alliage_resistant_chaud_Ni_Co  MES_tremp          TOLE              
3 F31-2017/01/03-1     F31       15        1 2017-01-03 03:26:00 2017-01-03 10:00:00 Alliage_resistant_chaud_Ni_Co  MES_tremp                            
4 F31-2017/01/03-2     F31      400        1 2017-01-03 10:16:00 2017-01-03 16:34:00 Alliage_resistant_chaud_Ni_Co  MES_tremp          TOLE              
5 F31-2017/01/03-3     F31      690        1 2017-01-03 17:42:00 2017-01-04 06:48:00 Alliage_resistant_chaud_Ni_Co     Revenu        CARTER              
6 F31-2017/01/04-1     F31    280.8        1 2017-01-04 14:17:00 2017-01-04 19:57:00       Acier_faiblement_alliés     Revenu        CARTER              
  FAMILLEPIECE3 FAMILLEPIECE4 FAMILLEPIECE5 FAMILLEPIECE6 TempsUtil heureDebut momentJournee JourLettre Jour
1                                                          5.033333         13         Matin        lun    1
2                                                          6.083333         20    Apres-midi        lun    1
3                                                          6.566667          3          Soir        mar    2
4                                                          6.300000         10         Matin        mar    2
5                                                         13.100000         17    Apres-midi        mar    2
6                                                          5.666667         14    Apres-midi        mer    3


f2C <- function(ValidFab) {
  TU <- readRDS("TU.rds")
  if (ValidFab) {
    TU <- TU[TU$VALIDFAB==0,]
  }
  TU$JourLettre <- factor(TU$JourLettre,levels=c("lun","mar","mer","jeu","ven","sam","dim"))
  for (i in c("lun","mar","mer","jeu","ven","sam","dim")) {
    assign(paste0("TU",i),TU[TU$JourLettre==i,])
    assign(i,
           ggplot(get(paste0("TU",i)),aes(x=TempsUtil)) + 
             stat_ecdf(geom = "point") + coord_cartesian(xlim=c(0,24)) + 
             geom_hline(yintercept = 0.8,color="red") + 
             geom_hline(yintercept = 0.5,color="blue") + 
             theme_linedraw() + annotate(geom="text",x=12,y=0.1,label=i) + ggtitle(i) +
             annotate(geom="text",x=2,y=.85,label = round(quantile(get(paste0("TU",i))$TempsUtil,0.8),2),col="red") + 
             annotate(geom="text",x=7,y=.55,label = round(quantile(get(paste0("TU",i))$TempsUtil,0.5),2),col="blue") + 
             theme(strip.background = element_rect(fill="white",colour="white"),
                   strip.text = element_text(color="black")))
    assign(i,ggplotly(get(i)))
  }
  if (ASauvegarder) {
    if (ValidFab) {
      AAfficher <- subplot(lun,mar,mer,jeu,ven,sam,dim, nrows=3,heights=c(0.3,0.3,0.3),margin=0.023) %>%
       layout(title="Temps d'utilisation en cumule par jour avec ValidFab=0")
      nf <- paste0(year(now()),"-",month(now()),"-",day(now())," ",hour(now()),"h",minute(now()))
      setwd(paste0(repExe,"/f2CV"))
      htmlwidgets::saveWidget(as_widget(AAfficher),paste0(nf, ".html"))
      setwd(repExe)
      AAfficher
    } else {
      AAfficher <- subplot(lun,mar,mer,jeu,ven,sam,dim, nrows=3,heights=c(0.3,0.3,0.3),margin=0.023) %>%
        layout(title="Temps d'utilisation en cumule par jour")
      nf <- paste0(year(now()),"-",month(now()),"-",day(now())," ",hour(now()),"h",minute(now()))
      setwd(paste0(repExe,"/f2C"))
      htmlwidgets::saveWidget(as_widget(AAfficher),paste0(nf, ".html"))
      setwd(repExe)
      AAfficher
    }
  } else {
    if (ValidFab) {
      subplot(lun,mar,mer,jeu,ven,sam,dim, nrows=3,heights=c(0.3,0.3,0.3),margin=0.023) %>%
        layout(title="Temps d'utilisation en cumule par jour avec ValidFab=0")
    } else {
      subplot(lun,mar,mer,jeu,ven,sam,dim, nrows=3,heights=c(0.3,0.3,0.3),margin=0.023) %>%
        layout(title="Temps d'utilisation en cumule par jour")
    }
  }
}

> lun

image

> AAfficher

image

Hi @Christophe,

Could you please turn this into a self-contained reprex (short for reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff. And, with plots it's especially helpful, as the output will automatically include the images.

install.reprex("reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

What to do if you run into clipboard problems

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.

Minor note: the images that were uploaded already got caught up in the code formatting. I've fixed that so that the uploads display, at least.

To clarify, the titles that are missing from the multi-panel plot are the ones with the days of the week? (Because it looks like the overall title — "Temps d'utilisation en cumule par jour" — is present in the multi-panel plot and absent in the single plot)

I agree with @mara that this will be a lot easier to debug if you can make it into a reprex! You seem to be pretty close. You would need to:

2 Likes

I have never use reprex so I hope it's OK

library(reprex)
#> Warning: le package 'reprex' a été compilé avec la version R 3.4.4
library(ggplot2)
library(plotly)
#> Warning: le package 'plotly' a été compilé avec la version R 3.4.4
#> 
#> Attachement du package : 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout

RIF <- data.frame(InterFournee = runif(700,min=0,max=8),JourLettre=c(rep("lun",100),rep("mar",100),rep("mer",100),rep("jeu",100),rep("ven",100),rep("sam",100),rep("dim",100)))
for (i in c("lun","mar","mer","jeu","ven","sam","dim")) {
  assign(paste0("RI",i),RIF[RIF$JourLettre==i,])
  assign(i,
         ggplot(get(paste0("RI",i)),aes(x=InterFournee)) + 
           stat_ecdf(geom = "point") + ggtitle(i))
  assign(i,ggplotly(get(i)))
}
# You can uncomment the three following lines 
# lun # there is a title at the top of the graph
# subplot(lun,mar,mer,jeu,ven,sam,dim, nrows=3,heights=c(0.3,0.3,0.3),margin=0.023) %>%
#   layout(title="Interfournees en cumule par jour") # There is no title at the top of each graphs


# sessionInfo()
# R version 3.4.3 (2017-11-30)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 7 x64 (build 7601) Service Pack 1

# Matrix products: default

# locale:
# [1] LC_COLLATE=French_France.1252  LC_CTYPE=French_France.1252    LC_MONETARY=French_France.1252 LC_NUMERIC=C                   LC_TIME=French_France.1252    

# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     

# other attached packages:
# [1] plotly_4.7.1  ggplot2_3.0.0

# loaded via a namespace (and not attached):
#  [1] Rcpp_0.12.15        bindr_0.1           magrittr_1.5        tidyselect_0.2.4    munsell_0.4.3       xtable_1.8-2        viridisLite_0.3.0  
#  [8] colorspace_1.3-2    R6_2.2.2            rlang_0.2.1         httr_1.3.1          plyr_1.8.4          dplyr_0.7.4         tools_3.4.3        
# [15] grid_3.4.3          data.table_1.10.4-3 gtable_0.2.0        withr_2.1.2         crosstalk_1.0.0     htmltools_0.3.6     yaml_2.1.16        
# [22] lazyeval_0.2.1      assertthat_0.2.0    digest_0.6.15       tibble_1.4.2        bindrcpp_0.2        shiny_1.0.5         tidyr_0.8.1        
# [29] purrr_0.2.4         htmlwidgets_1.0     mime_0.5            glue_1.2.0          labeling_0.3        compiler_3.4.3      pillar_1.1.0       
# [36] scales_0.5.0        jsonlite_1.5        httpuv_1.3.5        pkgconfig_2.0.1  

Created on 2018-07-19 by the reprex package (v0.2.0).

1 Like

Nice job getting the reprex together.

I don't know why the ggtitle info isn't carried through. However, I've used a sort of "trick" to add titles to single plots via faceting in the past that appears to work here.

In this case you can facet by JourLettre. Since there is only one value per data subset, this has the effect of adding a "title" via the facet strip.

I did this in lapply() to put your plots into a list to use in subplot(), but the fundamental idea is the same. You see I removed ggtitle() but added a line for facet_wrap().

plots = lapply(c("lun","mar","mer","jeu","ven","sam","dim"), function(i) {
    ggplotly( ggplot(RIF[RIF$JourLettre==i, ], aes(x = InterFournee)) +
                  stat_ecdf(geom = "point") +
                  facet_wrap(~JourLettre) )
})

These strip titles stay when putting things into subplot().

subplot(plots, nrows=3,heights=c(0.3,0.3,0.3),margin=0.023) %>%
       layout(title="Interfournees en cumule par jour")

4 Likes

Thank you very much for your answer @aosmith! It's working!
I do know facet functions, but I haven't used it because data is already subsetting by JourLettre. It's redundant but necessary !
Thank you all !

1 Like