Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Get unlimited access to the best of Medium for less than $1/week. Become a member

5 Ways to Enhance Your GGPLOT


Visualizations
Dan Larson · Follow
7 min read · Jul 19, 2023

13

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 1 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

GGplot2 is a powerful data visualization package in R and has become a


popular choice among data scientists and analysts for creating compelling
and informative visualizations. While ggplot2 offers a wide range of
customization options, there are certain techniques and practices that can
take your visualizations to the next level. In this post, I will explore five
ways to make your ggplot visualizations better, helping you communicate
your data insights effectively and captivate your audience.

Using Themes
One way to improve your ggplot visualizations is by leveraging themes.
Themes provide a consistent and professional appearance to your plots by

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 2 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

adjusting elements such as grid lines, axis labels, and backgrounds. By


choosing a theme that aligns with your visualization’s purpose, you can
make your plots more visually appealing and easier to interpret. Using
themes with you plots can give them a more professional look.

Themes are added to a plot by adding the theme at the end of your plot
code. p + theme_minimal() . If there other custom theme elements that you
want to add you should do that after calling the theme function.

ggplot themes
GGPLOT comes with a built-in set of themes. Below are some of the ones
that I like the most. You can see how using them improves the look of the
plot without doing much.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 3 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

ggthemes
Theme packages like ggthemes can help you easily recreate popular
charting styles.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 4 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

tvthemes
Tvthemes is a fun package that uses a the colors from popular tv shows to
syle your charts.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 5 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Using themes is an easy way to quickly improve the look and feel of your
charts. You can also develop your own themes by creating a function that
defines the theme elements you need.

library(ggplot2)

custom_theme <- function() {


#theme_minimal() %+replace%
theme(
# Define the overall appearance of the plot
plot.background = element_rect(fill = "white"),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(color = "lightgray"),
panel.grid.minor = element_blank(),

# Define the appearance of the axes

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 6 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

axis.line = element_line(color = "black"),


axis.text = element_text(color = "black"),
axis.title = element_text(color = "black", size = 12, face = "bold"),
axis.ticks = element_line(color = "black"),

# Define the appearance of the legend


legend.background = element_rect(fill = "white"),
legend.title = element_text(color = "black", size = 10, face = "bold"),
legend.text = element_text(color = "black", size = 10),

# Define the appearance of the plot title and subtitles


plot.title = element_text(color = "black", size = 14, face = "bold"),
plot.subtitle = element_text(color = "black", size = 12),

# Define the appearance of the plot labels


plot.caption = element_text(color = "black", size = 10),
plot.tag = element_text(color = "black", size = 11, face = "bold"),

# Remove the plot border


panel.border = element_blank()
)
}

p <- ggplot(mtcars, aes(x = mpg, y = wt, color = factor(cyl))) +


geom_point() +
labs(title = "Custom Theme Example", subtitle = "Scatter plot of mpg vs. wt")
custom_theme()

print(p)

Color Selection
Colors play a vital role in data visualization, as they can convey meaning,
highlight patterns, and differentiate between data points. When choosing
colors for your ggplot visualizations, consider the nature of your data and
the message you want to convey. You should avoid using colors that are too
similar or clash with each other, as this can lead to confusion. Instead, opt

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 7 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

for color palettes that are visually pleasing and effectively represent your
data.

Using the teamcolors package, I am easily able to add the NBA team primary
color to the chart with only a single extra line to my chart. By looking at the
two charts below you can see how using the NBA colors makes it easier for
the viewer to get meaning from the data. The legend is less important
because you can get a general sense of what the team is by the color of the
point.

Using a color theme is as easy as identifying a pallet and using the


scale_color_fill() or scale_color_manual() functions.

List of color pallet packages for ggplot:

1. RColorBrewer: Provides color palettes from the ColorBrewer website.


This package offers a variety of qualitative, sequential, and diverging
color schemes.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 8 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

2. viridis: Offers color maps that are perceptually uniform and suitable for
both colorblind individuals and printing in black and white.

3. viridisLite: A lightweight version of the viridis package, providing the


same color maps but with fewer dependencies.

4. wesanderson: Contains color palettes inspired by the films of Wes


Anderson, providing a distinctive and aesthetically pleasing set of
colors.

5. ggsci: Offers color palettes inspired by scientific journals, such as the


colors used in Nature, Science, and the New England Journal of
Medicine.

6. nord: Provides color palettes inspired by the Nord color scheme,


offering a modern and elegant set of colors.

7. iWantHue: Allows you to generate and explore color palettes based on


various criteria, such as the number of colors and the desired color
harmony.

8. colorspace: Offers color palettes based on the HCL (Hue-Chroma-


Luminance) color space, providing a perceptually uniform and visually
appealing set of colors.

9. dichromat: Provides color palettes specifically designed for individuals


with color vision deficiencies, offering enhanced accessibility for data
visualization.

10. ggthemes: Offers color palettes and themes inspired by popular data
visualization libraries and software, such as Excel, Tableau, and Stata.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 9 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Adding Jitter to Points


When working with point-based plots, such as scatter plots, adding jitter
can help prevent overplotting. Overplotting occurs when multiple data
points overlap, making it difficult to distinguish individual points. By
introducing a small amount of random noise to the position of the points,
you can spread them out, improving visibility and allowing for a more
accurate representation of the data.

I like to use this when I am looking at a very large dataset or the data are
highly concentrated. By showing all the points in a plot, you are able to
really demonstrate the distributions and see any irregularities in the data.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 10 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Free axis on faceted charts


Faceting is a powerful feature in ggplot that allows you to create multiple
panels or subplots based on different variables. However, by default, the
axes of the subplots are constrained to the same scale, which can
sometimes distort
Search
the visual representation of the data. To Write
overcome this,
you can free the axes by specifying scales=”free" in the facet_wrap or

facet_grid() function, ensuring that each panel’s axis scales independently.


This technique enables a more accurate comparison between the subplots
and facilitates better insights.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 11 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Iterate and Refine


Improving your ggplot visualizations is an iterative process. It’s essential to
experiment with different plot types, aesthetics, and configurations to find
the best representation for your data. Don’t be afraid to iterate, make
adjustments, and seek feedback from others. Refining your plots based on
insights gained from this process will lead to more compelling and effective
visualizations. All too often building a plot will take many attempts. I always
find that sharing my charts with friends and colleagues and explaining what
I am trying to show helps to improve the plot. It is also the best part.

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 12 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

By incorporating these tips into your ggplot visualizations, you can take
your data analysis to the next level. Whether you’re enhancing the
appearance, selecting appropriate colors, adding jitter to point-based plots,
freeing the axes when faceting, or iterating and refining, these strategies
will help you create visualizations that are both aesthetically pleasing and
informative. With practice and exploration, you’ll become proficient in
creating impactful ggplot visualizations that effectively communicate your
data insights.

If you are using RStudio, check out this addon package that lets you edit the
theme of your plot with a GUI interface.

Ggplot2 Data Science Visualization NBA R Programming

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 13 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Written by Dan Larson Follow

78 Followers

Data Engineer, father, and sixers fan in Philadelphia.

More from Dan Larson

Dan Larson Dan Larson

Three data tools I am going to Using the R and the Tidyverse to


learn more about 2023 analyze Joel Embiid’s Player Stats
2022 was a pretty exciting year in Data. The Joel Embiid is a professional basketball
field of data engineering really took off and… player from Cameroon who currently plays …
everyone is asking how it relates to data… the center for the Philadelphia 76ers of the
4 min read · Jan 2, 2023 6 min read · Feb 5, 2023
National…

25 1 8

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 14 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Dan Larson Dan Larson

Analyzing NBA Statistics with Three SQL Techniques to add to


DuckDB your toolkit
Data analysis is vital for extracting insights IMHO, SQL is one of the most important
and making informed decisions, even in… technical skills for data engineers, scientist…
sports. The NBA generates vast amounts of and analysts to develop. I have spent
7 that…· May 15, 2023
min read
data 4 min read · Dec 29, 2022
countless…

84 34

See all from Dan Larson

Recommended from Medium

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 15 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Sarose Parajuli Anjolaoluwa Ajayi in GDSC Babcock Dataverse

ChatGPT Cheat Sheet for Data 20% of EDA Plots Data Scientists
Science with R Use 80% of the Time
Data science is a field that combines domain All the plots you need to know for EDA
expertise, programming skills, and…
knowledge of mathematics and statistics to
2 min read · Dec 10, 2023
extract… 5 min read · Dec 22, 2023

674 2

Lists

Predictive Modeling w/ Practical Guides to Machine


Python Learning
20 stories · 746 saves 10 stories · 865 saves

Coding & Development data science and AI


11 stories · 355 saves 38 stories · 31 saves

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 16 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

Mokkup.ai Anmol Tomar in CodeX

The Future of Data Visualization: Top 10 Data Visualizations of 2023


2024 and Beyond Worth Looking at!
In this blog post, we will discuss the latest Level Up Your Visualization Game!
trends in data visualization and explore how…
these trends will shape the future of the field.
7 min read · Dec 15, 2023 · 4 min read · Dec 26, 2023

343 2 424 4

Allan victor Spencer Antonio Marlen-Starr

Visualizing Relationships with Stop Referring to the Gaussian


Venn Diagrams in R’s… Distribution as the “Normal”…
ggVennDiagram Package
Today, I will explain how you can create this Distribution
This article is related to several other articles
Venn diagram in R as simple as possible. I have recently published here, especially th…
one making the case for data scientists…
6 min read · Dec 18, 2023 · 3 min read · Dec 6, 2023

21 12 1

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 17 of 18
5 Ways to Enhance Your GGPLOT Visualizations | by Dan Larson | Medium 3/01/24, 09:13

See more recommendations

Help Status About Careers Blog Privacy Terms Text to speech Teams

https://medium.com/@danlarson_74954/5-ways-to-enhance-your-ggplot-visualizations-7ecb044fa9d9 Page 18 of 18

You might also like