Are you tired of bland and generic charts? Want to spice up your data visualizations with custom themes and logos? Look no further! In this blog post, we’ll walk you through the process of creating visually appealing charts using R, ggplot2, and some handy packages.
Step 1: Creating a Custom Color Palette
First things first, let’s create a color palette that matches your company’s branding. Suppose your company’s colors are navy blue, red, and gray. We can define the palette as follows:
nfl_palette <- c('#013369', '#D50A0A','#a0a0a0')
Step 2: Designing a Custom Theme
Next, let’s create a custom theme to remove unnecessary elements from our plots and make them look sleek and professional:
minimal_theme <- function() {
new_theme <- theme_minimal() +
theme(
panel.grid.major.y = element_blank(),
axis.line.x = element_line(color = "#CCCCCC", size = 1),
panel.grid.major.x = element_line(color = '#CCCCCC'),
panel.grid.minor = element_blank(),
plot.subtitle = element_text( face = 'italic'),
plot.title = element_text( face = 'bold'),
axis.title = element_text( face = 'bold'))
}
Step 3: Adding Company Logo
Now, let’s save the logo of your company to include it in your charts. Ensure you have the logo saved as an image file (e.g., logo.png).
Step 4: Creating a Function to Save High-Resolution Plots with Logo with
Finally, let’s create a function that saves our plots in high resolution and adds the company logo to them:
save_plot_as_png <- function(plot_to_save,
file_name,
plot_width = 600,
plot_height = 300,
logo_file_name,
...){
#Create the file name to be saved as a png file
the_file <- paste0(file_name, '.png')
#Create space to add the logo in the plot
the_plot <- the_plot +
ggplot2::labs(caption = '\n')
#1. Save the current chart
Cairo::CairoPNG(the_file, width = plot_width, height = plot_height, units = "px", res = 90)
print(the_plot)
dev.off()
#2. Load the chart and the logo
new_plot <- magick::image_read(the_file)
logo_raw <- magick::image_read(paste0(logo_file_name, '.png'))
#3. reshape the logo
logo <- logo_raw %>%
magick::image_scale("130") %>%
magick::image_background('transparent', flatten = TRUE)
#4. Join both
final_chart <- magick::image_composite(new_plot, logo, gravity = 'southeast' )
#5. Save
magick::image_write(final_chart, the_file)
}
Example Usage:
Now, let’s put it all together with an example:
#Sample data
data <- data.frame(
Pos = c('EDGE', 'LB', 'WR', 'OT', 'DT', 'RB', 'DE', 'QB'),
n = c(1,2,2,4,5,7,9,27),
type = c('Defense', 'Defense', 'Offense','Offense','Defense', 'Offense','Defense', 'Offense')
) %>%
mutate(
Pos = factor(Pos, levels = .$Pos)
)
# Create a plot
plot_object <- data %>%
ggplot(aes(x = Pos, y = n, fill = type)) +
geom_bar(stat = 'identity', width = .6) +
coord_flip() + minimal_theme() +
scale_fill_manual(values = nfl_palette) +
labs(
fill = NULL,
y = NULL,
x = NULL,
title = '1st Overall Picks: QBs Dominate the Field',
subtitle = 'During the Super Bowl era, quarterbacks have been selected 27 times\nas #1 overall pick.'
)
#Save the plot
save_plot_as_png(plot_to_save = plot_object,
file_name = '1st_round_overall_pick',
#Modify this, accordingly to your logo file
logo_file_name = 'nfl_logo.png'
)
With these steps, you can create stunning charts that perfectly match your company’s branding and make a lasting impression on your audience. Happy charting!
Leave a comment