Skip to content
R for the Rest of Us Logo

R for the Rest of Us Blog

How to make your own color palettes in ggplot

One of the great things about creating data viz with ggplot is that you can create color palettes that match your or your clients' branding. We've written about how we do this in our consulting work . But the way we work is fairly complicated and I was asked recently for a simpler solution to making custom ggplot color palettes. I recorded a video to show how to make three different types of palettes Qualitative (i.e. categorical) Sequential (going from a low to a high value) Diverging (going...

How to Make 250+ Plots at the Same Time with R

For the past several years, I've worked on a report called Oregon by the Numbers . In this project, I use R to make every single plot in the report: over 250 plots each year. In the time I've worked on this project, I've become more and more efficient at making these plots. My efficiency comes thanks to the walk() function from the purrr package. This function can be confusing, but if you learn how to use it, you too can make hundreds of plots in just a few lines of code. As you'll see in the...

Using ChatGPT to Create Data Visualization with ggplot

Have you heard about ChatGPT ? It's the latest in a line of AI tools that may transform how we write and how we code. ChatGPT takes a question and returns an answer. And it does so in a very human-sounding way . You know Siri? It's kind of like that, but actually good. To test it out, I signed up on the ChatGPT website and entered this prompt: Create a plot using ggplot2 and the palmerpenguins package . What I got back was very impressive. The response, copied verbatim below, shows exactly...

No Designer Needed: How to Create Beautiful Reports Using Only R

I recently gave a talk titled No Designer Needed: How to Create Beautiful Reports Using Only R at the CANSSI Ontario Statistical Software Conference . This talk showed how we at R for the Rest of Us use the pagedownpackage to make high-quality PDF reports for our clients . In the talk, I explained the two approaches people typically take when making PDFs: Some people make all charts and tables, and then bring in a designer to lay out the report. The problem with this is that it is...

Why You Should Always Use RStudio Projects

One of the most important tips we teach participants in R in 3 Months is to always use RStudio projects. There are a lot of resources out there that explain why this is useful (see, especially, Jenny Bryan's article Project-Oriented Workflow ). But many of these resources go a bit over the head of newcomers. So, I asked Charlie Hadley to record a video explaining why using RStudio projects is so important. In her video below, you'll see a clear, concise, and simple explanation for the value...

How to Read R Help Pages

Folks just starting out with R often assume that those of us who are more experienced never need to get help. This is definitely not true! Everyone always searches for help, no matter how advanced an R user you are. Learning how to search for help when you need it is a key skill. One of the places user typically look for help is in the help files that are loaded when you type ?function() (replacing function() with an actual function name, of course). These help files have a particular format...

Automating Sentences with R

The thing that first drew me to R was the ability to run the same analysis on different datasets and get a similar output every time: the same graphs, the same analyses, the same structure to the report, but different data, so a different story to tell. We often talk about the power of R in making research analyses reproducible , or in generating parameterized reports which all include variants of the same graphs, but one thing we haven't talked much about is the role R can play in automating...

How to Make a Donut Chart in ggplot

A lot of data visualizations are designed to show multiple data points (think bar charts that compare results for multiple groups). But what if you want to show just one data point? Say, for example, you want to show what percentage of people have accomplished something? There are two techniques we often use in our consulting work to show single data points: Donut charts : These are pie charts with a hole in the middle (hence, donuts). Pie charts with multiple slices are problematic (people...

Aligning Your Axes

At R For The Rest of Us, we spend a lot of time creating bespoke parameterised reports for our clients . In these reports, we often use CSS to create polished documents and to control the placement of our images. We've written about this approach elsewhere . In a recent project creating a needs assessment report on farmworkers in Oregon , we needed a bit of extra problem solving to make sure that the X axes of two plots, placed side by side, were aligned horizontally. This is the main thing...

How to merge data and calculate multilevel summaries

In one of the lessons in Fundamentals of R , I teach people how to use the group_by() and summarize() functions to calculate simple summaries (e.g. if I have population data on all states, what is the total population of the United States?). But what if you want to group by a group that's not in the original data frame? We got a question on this recently: Let's say I had state level data with numerical values per state. Let's say I wanted to assign each state to relevant regions, like...