Discover how to elevate your research presentations with these essential R plot exporting techniques.
Are you a PhD student struggling to save high-quality plots in R for your thesis or research presentations? You’re not alone. The journey of learning data visualization in R can be thrilling, yet exporting these visualizations effectively remains a challenge for many. If you’ve ever felt the frustration of presenting subpar graphics, this post is for you. I’ll unveil three transformative strategies that revolutionized how I export plots in R, promising to enhance your presentations and research documentation.
Learning data visualization in R is an exciting journey, but often, beginners face challenges in saving their plots effectively. When I first started, I was no different. Despite mastering various data visualization techniques, saving those plots for presentations or documentation was a struggle. My collaborators squinted at hard-to-read charts, PowerPoint presentations featured awkward-looking graphics, and more often than not, my plots were either cut off or pixelated, failing to blend seamlessly with their backgrounds.
However, with persistence and experimentation, I discovered methods to overcome these hurdles. I want to share these insights with fellow beginners in R, to save you from the frustrations I experienced. Here are the three key strategies I adopted:
Customized Themes for Different Mediums
Mastering ‘ggsave’
Efficient Code Snippets for Saving
I will delve into each of these 3 sections
I realized the importance of context when presenting data. What looks good in a Word document may not translate well to a PowerPoint slide. To address this, I set up separate themes for presentation plots and for documentation. This approach ensures that the visual aesthetics of the plots are tailored to the medium they are presented in, making them more effective and visually appealing.
For eg, this is my custom theme for ppt presentations.
# Plot with default theme
library(palmerpenguins)
ggplot(penguins, aes(x = bill_length_mm , y = body_mass_g)) +
geom_point() +
labs(title="Bill length and Body mass of Penguins",
subtitle = "From Palmer Penguins Dataset",
x="Length of the bill (in mm)",
y="Body mass (in grams)") +
theme_minimal()
# Example plot with the custom theme
ggplot(penguins, aes(x = bill_length_mm , y = body_mass_g)) +
geom_point() +
labs(title="Bill length and Body mass of Penguins",
subtitle = "From Palmer Penguins Dataset",
x="Length of the bill (in mm)",
y="Body mass (in grams)") +
theme_presentation()
You get the difference. Now for manuscripts and reports, I have a subdued theme.
# Custom theme for presentations
theme_doc <- function(
base_size=10) {
theme_minimal(base_size = base_size)+
theme(
plot.title = element_text(size = rel(1.6),face = "bold"),
plot.subtitle = element_text(size = rel(1.1),face = "italic"),
axis.text = element_text(size = rel(0.8))
)
}
# Example plot with the custom theme
ggplot(penguins, aes(x = bill_length_mm , y = body_mass_g)) +
geom_point() +
labs(title="Bill length and Body mass of Penguins",
subtitle = "From Palmer Penguins Dataset",
x="Length of the bill (in mm)",
y="Body mass (in grams)") +
theme_doc()
You get the point. I simply use the relevant theme when writing the manuscript, as my themes are predefined. There’s no need to stop and tweak them for presentations; I simply change to my designated presentation theme, and it’s all set.
The ‘ggsave’ function in R is a powerful tool for saving plots, but it can be complex for beginners. After several trials and errors, I mastered the nuances of this function. Key aspects like background color (‘bg’), format, resolution, and the use of relative paths (instead of absolute paths) for saving files are now second nature to me. Understanding these elements has significantly improved the quality and compatibility of my saved plots.
For example, I’ve outlined a basic version of the ggsave function’s arguments. Feel free to customize further according to your requirements. To explore more of its capabilities, simply type ?ggsave into your console.
my_penguin_plot <- ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
geom_point() +
ggtitle("Penguin Body Mass vs. Flipper Length") +
xlab("Flipper Length (mm)") +
ylab("Body Mass (g)") +
theme_minimal()
# Save the penguin plot using ggsave
ggsave(filename = "penguinplot.png",
plot = my_penguin_plot,
device = "png",
bg = "white")
Occasionally, I opt for last_plot() instead of specifically naming and selecting a plot to save. This method automatically saves the most recently generated plot. It’s important to note that this approach saves the plots in the root folder of the project. If you require your figures to be stored in a specific directory, I recommend consulting this blog post, where I delve into the use of relative paths for file storage.
Efficiency is key in data analysis, and this extends to saving plots. I created a code snippet in R that I can easily call upon whenever I need to save a plot. By typing savemyplot
a pre-written script with all the necessary commands appears. I then fill in the specific details for the plot I’m working on. This method not only saves time but also reduces the chances of errors that can occur with manual input.
For eg, I wrote this snippet with the basic commands I generally use while saving plots.
To create snippets, you need to navigate to Tools → Global Options, select “Code”, and then “Edit Snippets”. More details on snippets are available here.
You’ll see that when I start typing “savemy…”, my snippet is prompted, which makes my life easier as I don’t have to remember the commands every time I try to save the plots.
This function simplifies the saving process by only requiring you to specify the plot and filename for standard settings.
You can further customize this function to include different formats or themes based on the medium.
An interesting aspect to explore further is the range of file formats and customization options available with ‘ggsave’. This function is not just about saving a plot; it’s about preserving the integrity and aesthetic of your data visualization in various formats, be it PNG, PDF, SVG, or others.
I also write separate scripts for figures, which helps me track them much better. Recently, I started creating Quarto docs for figures that contain all my figures as they evolve, with a table at the beginning to track changes.
Saving plots in R may seem daunting at first, but with the right techniques and tools, it becomes a straightforward and rewarding part of the data visualization process. By setting up customized themes, using efficient code snippets, and mastering ‘ggsave’, you can ensure that your plots are not only saved correctly but also maintain their visual appeal across different platforms and formats.
For attribution, please cite this work as
Soundararajan (2024, Feb. 2). My R Space: 3 Game-Changing R Plot Export Tips for PhD Students - Excel in Your Thesis Presentation and Research!. Retrieved from https://github.com/soundarya24/SoundBlog/posts/2024-02-02-savingplot/
BibTeX citation
@misc{soundararajan20243, author = {Soundararajan, Soundarya}, title = {My R Space: 3 Game-Changing R Plot Export Tips for PhD Students - Excel in Your Thesis Presentation and Research!}, url = {https://github.com/soundarya24/SoundBlog/posts/2024-02-02-savingplot/}, year = {2024} }