R/plotting.R

Defines functions save_board_figs save_fig geo_col

Documented in geo_col save_board_figs save_fig

#' Color codes
#'
#' Last colour is grey75.
#'
#' @param n number of colours
#'
#' @export
#'
geo_col <- function(n){
  n <- n - 1
  cols <- c("#3b5998", "#FFCD00", "#000000", "#6E8B3D", "#6495ED", "#EE7600", "#8B3E2F", "#B22234", "#8968CD", "#7F7F7F")
  col_seg <- rep(1:length(cols), times = ceiling(n/length(cols)))[1:n]
  c(cols[col_seg], "grey75")
}


#' Save figures for report
#'
#' Uses ggsave.
#'
#' @param filename File name to create on disk.
#' @param plot Plot to save, defaults to last plot displayed.
#' @param width Plot size
#' @param height Plot size
#'
#' @export

save_fig <- function(filename,
                     plot = last_plot(),
                     width = 5.8,
                     height = 3.9){
  plot <- plot + theme(legend.text = element_text(size = 9))
  ggplot2::ggsave(filename = file.path("~/../Pellervon Taloustutkimus PTT ry/KT172 Kilpailukyvyn arviointi - General/Raportointi/Kuviot",
                                       paste0(filename, ".png")), plot = plot, width = width, height = height)
}




#' Save figures for competitiveness board report
#'
#' to Downloads folder
#'
#' Uses ggsave.
#'
#' @param filename File name to create on disk.
#' @param plot Plot to save, defaults to last plot displayed.
#' @param width Plot size
#' @param height Plot size
#' @param save_data A logical to save plot data.
#'
#' @export

save_board_figs <- function(filename,
                      plot = last_plot(),
                      width = 13.5,
                      height = 13.5,
                      save_data = FALSE){
  plot <- plot +
    ggptt::the_title_blank(c("x", "t", "l"))

  plot_slide <- plot +
    theme(text = element_text(size=16))

  ggplot2::ggsave(file.path(Sys.getenv("USERPROFILE"),"Downloads", paste0(filename, ".pdf")),
                  plot = plot, width = width, height = height, units = "cm")
  ggplot2::ggsave(file.path(Sys.getenv("USERPROFILE"),"Downloads", paste0(filename, ".png")),
                  plot = plot, width = width, height = height, units = "cm")
  ggplot2::ggsave(file.path(Sys.getenv("USERPROFILE"),"Downloads", paste0(filename, "_esitys.png")),
                  plot = plot_slide, width = 26, height = 12, units = "cm")
  if (save_data){
    pdat <- ggplot_build(plot)$plot$data
    write.csv2(pdat, file.path(Sys.getenv("USERPROFILE"),"Downloads", paste0(filename, "_data.csv")), row.names = FALSE)
  }
}
pttry/ficomp documentation built on June 11, 2024, 9:53 p.m.