R/HW_plot.R

Defines functions HW_plot

#ought to add fonts at first

HW_plot <- function(data, interval, title, file, min.cells = 1, renumber = TRUE) {

    data %<>% setDT() %$%
        .[year(date) %in% interval
        ][, if(.N >= min.cells) .SD, .(clusterId, date)
        ][, ':='(cent_lon = mean(lon), cent_lat = mean(lat)), .(clusterId, date)]
    if(renumber == TRUE) data[order(clusterId), clusterId := .GRP, clusterId]
    plot_row <- ceiling(uniqueN(data, by = 'date') / 6)

    p <- ggplot(data) +
        geom_sf(data = china_map, fill = 'white', color = '#939597', lwd = 0.1) +
        theme_void(base_size = 18) +
        geom_tile(mapping = aes(x = lon, y = lat, fill = clusterId)) +
        scale_fill_gradient(low = '#2EB0E8',
                            high = '#FE4880',
                            n.breaks = uniqueN(data, by = 'clusterId') / 10,
                            limits = c(min(data$clusterId), NA),  #限制图例刻度
                            guide = guide_colorbar(reverse = TRUE,
                                                   frame.colour = "white",
                                                   ticks.colour = "white",
                                                   frame.linewidth = 1.5,
                                                   ticks.linewidth = 1.5,
                                                   barheight = unit(6.4267 * plot_row - 1.68, 'cm'))) +  # 6.25x-1.8
        geom_text(mapping = aes(x = cent_lon, y = cent_lat, label = clusterId),
                  size = 4,
                  hjust = -0.2,
                  vjust = 0.9,
                  family = 'Poppins-SemiBold',
                  color = '#3B3141',
                  check_overlap = TRUE) +
        facet_wrap(~date, ncol = 6L) +
        ggtitle(title) +
        theme(text = element_text(family = 'Ubuntu-Regular'),
              plot.background = element_rect(fill = "#F5F4F6", color = NA),
              plot.title = element_text(margin = margin(b = 0.8, unit = 'cm'),
                                        hjust = 0.004,
                                        family = 'Ubuntu-Medium',
                                        size = 30,
                                        face = 'bold',
                                        color = '#3D3D3D'),
              strip.text.x = element_text(size = 16,
                                          hjust = 0.05,
                                          color = '#362D23',
                                          margin = margin(b = 0.1, unit = 'cm')),
              panel.spacing.y = unit(0.8, 'cm'),
              legend.box.spacing = unit(1.9, 'cm'),
              legend.title = element_blank(),
              legend.key.width = unit(0.7, 'cm'),
              plot.margin = margin(t = 1.2, b = 1.2, l = 3, r = 3, unit = 'cm'))
    p <- set_panel_size(p, height = unit(5, 'cm'), width = unit(7, 'cm'))

    cairo_pdf(file = file, width = unit(20.5, 'cm'), height = unit(2.53 * plot_row + 1.34, 'cm'))
    plot(p)
    dev.off()
}
CUG-hydro/heatwave documentation built on Dec. 17, 2021, 1:53 p.m.