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.
Mind-blowing.
— Jose Fernandez (@jfernandez__) December 4, 2022
Here is an #RStats Shiny App I built without writing a single line of code. Just through a text-based conversation with #ChatGPT.
...and all in under 3 minutes.
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.
Watch chatGPT create a ggplot2 plot for you with colors and legend (and explain how to do it!)
— Marek Rogala 🤖📊🚀 (@marekrog) December 1, 2022
The new best way to learn a new technology.#rstats #rshiny #ggplot2 @thomasp85 pic.twitter.com/OvYA9tsZhL
ChatGPT can also help with debugging, as Francisco RodrÃguez-Sánchez shows (with some caveats).
I'm seriously considering showing #ChatGPT (https://t.co/wqxZ5IyR2Z) to my #RStats students.
— F Rodriguez-Sanchez (@frod_san) December 5, 2022
I can see how ChatGPT can greatly help them learn & overcome errors (see 👇🏽). But there are probably some drawbacks too.
Thoughts? pic.twitter.com/UHG2GWV7js
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.
You need to be signed-in to comment on this post. Login.