Skip to content
R for the Rest of Us Logo

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 how to create a scatterplot with with ggplot2 and the palmerpenguins package.

ChatGPT response

To create a plot using ggplot2 and the palmerpenguins package, you would first need to install and load the packages into your R environment. You can do this by running the following commands:

# Install the packages
install.packages("ggplot2")
install.packages("palmerpenguins")

# Load the packages
library(ggplot2)
library(palmerpenguins)

Once the packages are installed and loaded, you can use the ggplot() function to create a basic scatter plot using the data argument to specify the data frame containing the data you want to plot. For example, to create a scatter plot of the body mass and flipper length for the penguins in the Palmer penguins dataset, you could use the following code:

ggplot(data = penguins, aes(x = body_mass_g, y = flipper_length_mm)) +
  geom_point()

This code would create a scatter plot with body mass on the x-axis and flipper length on the y-axis. You can customize the appearance of the plot by adding additional layers, such as a smoother line or different color schemes, using the geom_ functions provided by ggplot2. For more information and examples, you can refer to the ggplot2 documentation and tutorials online.

Does it work?

Ok, sounds reasonable! Does it work? I ran the code ChatGPT provided me and yes, it does indeed work!

What does this mean for us mortals?

On Twitter the last few days, R users have been gushing about ChatGPT. One had it make him a fully Shiny dashboard.

There is, understandably, some fear that tools like ChatGPT will make R users unnecessary. I think that's unlikely. Tools like ChatGPT can help us work faster by giving a headstart on code, but I doubt they will never know enough to know not just how to make data visualization but also good data viz.

Where ChatGPT is likely to prove beneficial is for R learners (a group, to be clear, that includes everyone who ever Googles an R question; i.e. all of us). My question about and the tweet by Marek Rogala below show how ChatGPT can teach you how ggplot2 (or any R package) works.

ChatGPT can also help with debugging, as Francisco Rodríguez-Sánchez shows (with some caveats).

Try out ChatGPT

If you're curious, as I was about ChatGPT, there's no better time than the present to try it out. It's in a free preview now so give it a shot and see if it can help you on your R journey.

Sign up for the newsletter

Get blog posts like this delivered straight to your inbox.

Let us know what you think by adding a comment below.

You need to be signed-in to comment on this post. Login.

David Keyes
By David Keyes
December 5, 2022

Sign up for the newsletter

R tips and tricks straight to your inbox.