Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RaynorJim opened this issue Jan 9, 2025 · 2 comments
Closed

Comments

@RaynorJim
Copy link

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
@teunbrand
Copy link
Collaborator

Thank for the report!
As these bars essentially have sub-pixel heights, I think this is just a limitation of the anti-aliasing capabilities of graphics devices.
You can use ragg::agg_png(snap_rect = FALSE) to have a png device that anti-aliases this correctly for rectangles that don't snap to the pixel grid.

In any case, there isn't anything that ggplot2 can do about this, as ggplot2 builds the graphics on top of {grid} which then hands it do the graphics device so there is no direct control. For this reason, I'll close this issue.

@teunbrand teunbrand closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2025
@RaynorJim
Copy link
Author

Thanks, using ragg::agg_png(snap_rect = FALSE) helped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants