Skip to content

Issue when generating png images with thin bars in a stacked bar chart #6278

Closed as not planned
@RaynorJim

Description

@RaynorJim

I identified a problem with stacked bar charts, whenever some of the bars are very thin compared to the others in a stacked barchart. Sometimes the thin bars are not visible in the stack, and it simply happens randomly at images with different heights. Here are a couple of example images and the reprex is given below for various different image heights.

First 2 stacks have the red bar visible:
Image

Stacks 1 and 3 have the red bar visible:
Image

All 3 stacks have the red bar visible:
Image

library(ggplot2)
df <- data.frame(
  x = c("a", "a", "b", "b", "c", "c"),
  y = c(2023, 4, 501, 4, 1543, 4),
  z = c('g', 'd', 'g', 'd', 'g', 'd')
)

p <- ggplot(
  data = df,
  mapping = aes(
    x = x,
    y = y,
    fill = z
  )
) +
  geom_col(
    group = 1,
    position = "stack"
  )

for (height in seq(350, 500, by = 10)) {

  png(
    filename = paste0('height=', height, ".png"),
    width = 1200,
    height = height,
    units = "px",
  )

  print(p)

  dev.off()
}

Created on 2025-01-09 with reprex v2.1.1

Session info
sessionInfo()
#> R version 4.4.2 (2024-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 22631)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_Europe.utf8  LC_CTYPE=English_Europe.utf8   
#> [3] LC_MONETARY=English_Europe.utf8 LC_NUMERIC=C                   
#> [5] LC_TIME=English_Europe.utf8    
#> 
#> time zone: Europe/Berlin
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] ggplot2_3.5.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] vctrs_0.6.5       cli_3.6.3         knitr_1.49        rlang_1.1.4      
#>  [5] xfun_0.50         glue_1.8.0        labeling_0.4.3    colorspace_2.1-1 
#>  [9] htmltools_0.5.8.1 scales_1.3.0      rmarkdown_2.29    grid_4.4.2       
#> [13] evaluate_1.0.1    munsell_0.5.1     tibble_3.2.1      fastmap_1.2.0    
#> [17] yaml_2.3.10       lifecycle_1.0.4   compiler_4.4.2    fs_1.6.5         
#> [21] pkgconfig_2.0.3   rstudioapi_0.17.1 farver_2.1.2      digest_0.6.37    
#> [25] R6_2.5.1          reprex_2.1.1      pillar_1.10.1     magrittr_2.0.3   
#> [29] tools_4.4.2       withr_3.0.2       gtable_0.3.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions