For loop seems to be affecting chart plotting

I have some code which plots a spine chart in a 'for' loop . The chart should have black lines between the domains according to the show_dividers = "outer" argument in the area_profiles function of fingertipscharts. Running the code on its own for one spine chart creates the chart with dividers, running the code in the loop does not. Sorry for the length of this but it is mostly arguments (data is public). Does anyone have any idea why that might be:

library(fingertipscharts)
library(ggplot2)

df_pop1 <- read.csv("https://github.com/Jon51/jonw/raw/master/df_pop1.csv")
gp <- c("GP1", "GP2", "GP3", "GP4")
for(i in gp){
s <- area_profiles(df_pop1,
value = Value,
count = Count,
area_code = AreaCode,
local_area_code = i,
indicator = IndicatorNm,
timeperiod = Timeperiod,
polarity = Polarity,
significance = Significance,
area_type = AreaType,
median_line_area_code = "CCG1",
header_positions = c(-1.43, -0.80, -0.6, -0.4, -0.2, -0.05, 1.1),
header_labels = c("Indicator", "Time\nperiod", "Local\ncount","Local\nvalue", "Bromley\nvalue", "Lowest", "Highest"),
datatable = TRUE,
relative_point_size = 2,
relative_domain_text_size = 0.85,
relative_text_size = 1.2,
bar_width = 0.9,
indicator_label_nudgex = -0.5,
show_dividers = "outer",
domain = Domain)
plot1 <- paste("spine", i, ".png", sep = "")
ggsave(filename = plot1, width = 10, height = 15, dpi = 300)
}

s <- area_profiles(df_pop1,
value = Value,
count = Count,
area_code = AreaCode,
local_area_code = "GP1",
indicator = IndicatorNm,
timeperiod = Timeperiod,
polarity = Polarity,
significance = Significance,
area_type = AreaType,
median_line_area_code = "CCG1",
header_positions = c(-1.43, -0.80, -0.6, -0.4, -0.2, -0.05, 1.1),
header_labels = c("Indicator", "Time\nperiod", "Local\ncount","Local\nvalue", "Bromley\nvalue", "Lowest", "Highest"),
datatable = TRUE,
relative_point_size = 2,
relative_domain_text_size = 0.85,
relative_text_size = 1.2,
bar_width = 0.9,
indicator_label_nudgex = -0.5,
show_dividers = "outer",
domain = Domain)
s
plot <- paste("divider", ".png", sep = " ")
ggsave(filename = plot, width = 10, height = 15, dpi = 300)

I am plotting 60 indicators in my spine chart and the black line is not appearing although I can see there is a wider white gap where it should be.

Hi @Jonw!

Thanks for including a self-contained, reproducible example that made it easy for me to poke at this problem! :grinning: Oddly enough, the dividing lines re-appear if I explicitly pass the plot object to ggsave() inside the loop:

library(ggplot2)
library(fingertipscharts)
#> Registered S3 method overwritten by 'geojsonio':
#>   method         from 
#>   print.location dplyr

df_pop1 <- read.csv("https://github.com/Jon51/jonw/raw/master/df_pop1.csv")
gp <- c("GP1", "GP2", "GP3", "GP4")

# For loop
for(i in gp){
  s <- area_profiles(df_pop1,
                     value = Value,
                     count = Count,
                     area_code = AreaCode,
                     local_area_code = i,
                     indicator = IndicatorNm,
                     timeperiod = Timeperiod,
                     polarity = Polarity,
                     significance = Significance,
                     area_type = AreaType,
                     median_line_area_code = "CCG1",
                     header_positions = c(-1.43, -0.80, -0.6, -0.4, -0.2, -0.05, 1.1),
                     header_labels = c("Indicator", "Time\nperiod", "Local\ncount","Local\nvalue", "Bromley\nvalue", "Lowest", "Highest"),
                     datatable = TRUE,
                     relative_point_size = 2,
                     relative_domain_text_size = 0.85,
                     relative_text_size = 1.2,
                     bar_width = 0.9,
                     indicator_label_nudgex = -0.5,
                     show_dividers = "outer",
                     domain = Domain)
  plot_name <- paste("loop_spine", i, ".png", sep = "")
  ggsave(plot = s, filename = plot_name, width = 10, height = 15, dpi = 300)
}
#> Warning: Removed 6 rows containing missing values (geom_text).

#> Warning: Removed 6 rows containing missing values (geom_text).

#> Warning: Removed 6 rows containing missing values (geom_text).

#> Warning: Removed 6 rows containing missing values (geom_text).

# Include sample file output in this reprex
knitr::include_graphics("loop_spineGP1.png")

Created on 2019-08-25 by the reprex package (v0.3.0)

Session info
sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 16.04.6 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/atlas-base/atlas/libblas.so.3.0
#> LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] fingertipscharts_0.0.8 ggplot2_3.2.0         
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.1            lattice_0.20-38       tidyr_0.8.3          
#>  [4] png_0.1-7             class_7.3-15          assertthat_0.2.1     
#>  [7] zeallot_0.1.0         digest_0.6.20         V8_2.3               
#> [10] mime_0.7              plyr_1.8.4            R6_2.4.0             
#> [13] backports_1.1.4       evaluate_0.14         e1071_1.7-2          
#> [16] httr_1.4.0            geojson_0.3.2         highr_0.8            
#> [19] pillar_1.4.2          rlang_0.4.0           lazyeval_0.2.2       
#> [22] curl_4.0              miniUI_0.1.1.1        geojsonio_0.7.0      
#> [25] DT_0.8                rmarkdown_1.14        rgdal_1.4-4          
#> [28] jqr_1.1.0             readr_1.3.1           fingertipsR_0.2.7    
#> [31] stringr_1.4.0         foreign_0.8-71        htmlwidgets_1.3      
#> [34] munsell_0.5.0         shiny_1.3.2           compiler_3.6.0       
#> [37] httpuv_1.5.1          xfun_0.8              pkgconfig_2.0.2      
#> [40] lemon_0.4.3           rgeos_0.5-1           htmltools_0.3.6      
#> [43] tidyselect_0.2.5      gridExtra_2.3         tibble_2.1.3         
#> [46] crayon_1.3.4          dplyr_0.8.3           withr_2.1.2          
#> [49] later_0.8.0           sf_0.7-7              grid_3.6.0           
#> [52] jsonlite_1.6          xtable_1.8-4          gtable_0.3.0         
#> [55] DBI_1.0.0             magrittr_1.5          units_0.6-4          
#> [58] scales_1.0.0          KernSmooth_2.23-15    stringi_1.4.3        
#> [61] reshape2_1.4.3        promises_1.0.1        leaflet_2.0.2        
#> [64] sp_1.3-1              xml2_1.2.0            vctrs_0.2.0          
#> [67] tools_3.6.0           glue_1.3.1            purrr_0.3.2          
#> [70] crosstalk_1.0.0       shinycssloaders_0.2.0 hms_0.5.0            
#> [73] yaml_2.2.0            colorspace_1.4-1      maptools_0.9-5       
#> [76] classInt_0.4-1        knitr_1.23

I don't know anything about fingertipscharts, so I don't know whether this is a problem with how fingertipscharts is constructing the plot or a bug in in ggsave(), but you might consider posting a bug report to the fingertipscharts repo, to start with.

1 Like

Dear jcblum,
Thank you so much. That's sorted it. Much appreciated. That was driving me mad.
Jonw

1 Like

I can imagine! Mysterious plotting gremlins can really make you tear your hair out.

Since it sounds like your question's been answered, would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.