0

How to get the ggplot double bar graph as a GIF file with the following specifications - DPI of 300 and Width of 440 pixels.

I use the following code and data for making the graph

graph_text <- structure(list(
  percentage = c(57.14, 29.76, 69.32, 28.41, 57.89, 34.21, 58.59, 33.33, 48.42, 42.11, 59.77, 29.89, 72.13, 18.03, 53.33, 33.33, 55.1, 40.82, 46.55, 37.93),
  year = c(2020L, 2020L, 2019L, 2019L, 2018L, 2018L, 2017L, 2017L, 2016L, 2016L, 2015L, 2015L, 2014L, 2014L, 2013L, 2013L, 2012L, 2012L, 2011L, 2011L),
  gender = c("male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female", "male", "female")
),
class = "data.frame", row.names = c(NA, -20L)
)

ymax <- max(graph_text$percentage)
ggplot(aes(x = year, y = percentage, color = gender, fill = gender), data = graph_text) +
  geom_bar(position = "dodge", stat = "identity") +
  theme_classic() +
  geom_text(aes(label = percentage), size = 4, position = position_dodge(width = 1.1), vjust = -0.2) +
  scale_y_continuous(limits = c(0, 1.4 * ymax))
Session info - 
R version 4.1.0 (2021-05-18)Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:[1] LC_COLLATE=English_India.1252  LC_CTYPE=English_India.1252    LC_MONETARY=English_India.1252[4] LC_NUMERIC=C                   LC_TIME=English_India.1252    
attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     
loaded via a namespace (and not attached): 
[1] fansi_0.5.0      assertthat_0.2.1 dplyr_1.0.6      crayon_1.4.1     utf8_1.2.1       
[6] grid_4.1.0       R6_2.5.0         DBI_1.1.1        lifecycle_1.0.0  gtable_0.3.0    
[11] magrittr_2.0.1   scales_1.1.1     ggplot2_3.3.3    pillar_1.6.1     rlang_0.4.11    
[16] generics_0.1.0   vctrs_0.3.8      ellipsis_0.3.2   tools_4.1.0      glue_1.4.2      
[21] purrr_0.3.4      munsell_0.5.0    compiler_4.1.0   pkgconfig_2.0.3  colorspace_2.0-1
[26] tidyselect_1.1.1 tibble_3.1.2    
stefan
  • 90,330
  • 6
  • 25
  • 51
BR27
  • 21
  • 4
  • 1
    [`?ggsave`](https://ggplot2.tidyverse.org/reference/ggsave.html) lists argument defaults of `ggsave(..., dpi=300, width=NA)`, you can changes those to your intended values. – r2evans Aug 24 '21 at 21:39
  • This only allows me to adjust the width in inches/cm, not in pixels. Also, I am referring to the following article - https://stackoverflow.com/questions/38907514/saving-a-high-resolution-image-in-r but the image I am getting is just one part of the entire graph – BR27 Aug 24 '21 at 21:55
  • From that link: *Plot size in units ("in", "cm", "mm", or "px")*. Pixels are "px". Am I misinterpreting it? – r2evans Aug 25 '21 at 00:56
  • Seems like you should be able to use `ggsave()`, and yet it seems that "gif" is not one of the native graphics drivers in R. Odd. Regardless, it seems your [question may already have been answered](https://stackoverflow.com/questions/18317721/saving-plots-in-r-as-gifs). – chemdork123 Aug 25 '21 at 05:07
  • Thanks everyone for the input, I managed to solve the problem by reducing the size of the title of my graph. While I was getting an output using my code as a jpeg, it was very blurred. Reducing the length of the title made the image much better. And thanks for the link to the article on how to save it as gif – BR27 Aug 25 '21 at 20:05

0 Answers0