Advertisement
karstenw

Carousel

Jan 16th, 2023
2,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.66 KB | None | 0 0
  1. carousel <- function(fn="Rplots.pdf") {
  2.     stopifnot(require(ggplot2))
  3.     stopifnot(require(aRtsy))
  4.     stopifnot(require(meme))
  5.     stopifnot(require(ggwordcloud))
  6.     stopifnot(require(gridExtra))
  7.     stopifnot(require(ggtext))
  8.  
  9.     # page 1
  10.     set.seed(13)
  11.     title_colour <- colorPalette("mixer4")[3]
  12.     yellow <- colorPalette("mixer4")[1]
  13.     p1 <- canvas_flow(
  14.         colors = colorPalette("mixer4"),
  15.         polar=T, background = "#EFEFEF",
  16.         lwd=2
  17.     ) +
  18.     ggtitle("Create Carousel\nPosts with R") +
  19.     theme(plot.title = element_text(
  20.             face="bold", colour=title_colour, size=42, vjust=-0.5
  21.         )
  22.     ) +
  23.     theme(plot.margin = unit(c(1,0.5,0,0), "cm"))
  24.  
  25.     # page 2
  26.     tools <- data.frame(word=c(
  27.         "Photoshop", "Canva", "Visme", "Spark", "Design Wizard", "Easil",
  28.         "Snappa", "Crello", "Stencil", "PicMonkey", "Vectornator", "Visme",
  29.         "Stencil", "Snappa", "Piktochart", "Design Wizard", "Easil", "Pixelied",
  30.         "Tyle", "Pixlr", "Fotojet", "Fotor", "Venngage", "PicMonkey",
  31.         "Desygner", "Figma", "Penpot", "Affinity Publisher", "Vectr", "InDesign",
  32.         "Fireworks", "Polotno Studio", "Genially", "PicMonkey", "Corel Vector",
  33.         "PosterMyWall", "PixTeller", "VistaCreate", "Venngage", "Simplified",
  34.         "reThumbnail", "Cretorial", "Lucidpress", "Baseline", "Picofme.io",
  35.         "EvolveUI", "Drawtify", "VivaDesigner", "Pikiz", "Tallery Gallery",
  36.         "MockoFun", "Picmaker", "Plasfy", "APITemplate.io", "SoVisual.co",
  37.         "Pozters", "Mediamodifier", "Dezo", "365Layouts.com", "Sprightly",
  38.         "Etimat Designer"
  39.     ))
  40.     p2 <- ggplot(tools, aes(
  41.             label = word,
  42.             color = factor(sample(5, nrow(tools), replace = T))
  43.         )) +
  44.         scale_colour_manual(values=colorPalette("mixer4")) +
  45.         geom_text_wordcloud_area(shape="square") +
  46.         scale_size_area(max_size = 8) + theme_minimal() +
  47.         ggtitle("If you think you \nneed a special tool...") +
  48.         theme(plot.title = element_text(
  49.             face="bold", colour=title_colour, size=42, vjust=-0.5
  50.         )) +
  51.         theme(plot.margin = unit(c(1,0.5,0,0.5), "cm"))
  52.    
  53.     # page 3
  54.     dat <- data.frame(x = 1, y = 1,
  55.         label = paste0(
  56.             "<span style='color: red; font-size: 100pt'>It's just a PDF!</span><br>",
  57.             paste0(
  58.                 "<span style='color: ", title_colour, "; font-size: 20pt'>@ 1080x1080px.<br><br>It always has been.</span>"
  59.             )
  60.         )
  61.     )
  62.     p3 <- ggplot(dat) +
  63.     ggtext::geom_textbox(
  64.         aes(x = x, y = y, label = label), box.colour = NA, width = unit(10, "cm"),
  65.         fill=yellow
  66.     ) +
  67.     theme_void() +
  68.     theme(
  69.         panel.background = element_rect(fill = yellow, colour = NA, size = 0),
  70.         panel.border = element_blank(),
  71.         panel.grid.major = element_blank(),
  72.         panel.grid.minor = element_blank()
  73.     ) +
  74.     ggtitle("... then you are mistaken:") +
  75.     theme(plot.title = element_text(
  76.         face="bold", colour=title_colour, size=42, vjust=-0.5
  77.     )) +
  78.     theme(plot.margin = unit(c(1,0.5,0,0.5), "cm")) +
  79.     theme(plot.background = element_rect(fill = yellow))
  80.        
  81.     # page 4
  82.     credits <- c(
  83.         "aRtsy: Generative Art with 'ggplot2'",
  84.         "ggwordcloud: A Word Cloud Geom for 'ggplot2'",
  85.         "ggtext: Improved Text Rendering Support for 'ggplot2'"
  86.     )  
  87.     n_lines <- length(credits)
  88.     dat <- data.frame(x = 1, y=n_lines:1, label = credits)
  89.     p4 <- ggplot(dat) +
  90.         ggtext::geom_textbox(
  91.             aes(x = x, y = y, label = label),
  92.             box.colour = NA, width=unit(500, "pt"), fill="#EFEFEF",
  93.             size=8
  94.         ) + ylim(-2, n_lines+2) +
  95.         theme_void() +
  96.         ggtitle("Credits") +
  97.         theme(plot.title = element_text(
  98.             face="bold", colour=title_colour, size=42, vjust=-0.5
  99.         )) +
  100.         theme(plot.margin = unit(c(1,0.5,0,0.5), "cm"))
  101.      
  102.     # save it
  103.     res <- marrangeGrob(list(p1, p2, p3, p4), nrow=1, ncol=1, top=NULL)
  104.     ggsave(fn,
  105.         plot=res, device="pdf", width=1080, height=1080, units="px", dpi=72,
  106.         bg="#EFEFEF", scale=0.5
  107.     )
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement