• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Seaborn
  • Matplotlib
  • ggplot2
  • Altair
  • About
    • Privacy Policy
  • Visualizing Activation Functions in Neural Networks
  • Confusion Matrix Calculator
  • Visualizing Dropout Rate in Neural Network
  • Visualizing Loss Functions in Neural Networks
  • Show Search
Hide Search

10 ChatGPT Prompts for ggplot2 Boxplots: Complete Guide with Working R Code

datavizpyr · September 7, 2025 ·

Boxplots are one of the most effective ways to summarize data distributions in R, and ggplot2 makes them both powerful and flexible. ggplot2 is the go-to package for making boxplots in R.

What’s new is how you can now create them by working with AI tools like ChatGPT. Instead of working alone, now we can work with AI tools like ChatGPT/Gemini/Claude and it can completely change the way you code in R.

By blending AI-driven workflow/prompts with reproducible code, you can speed up your workflow and create publication-quality visualizations faster than ever. Whether you’re learning ggplot2 or refining your workflow, these examples are a practical starting point.

While AI-generated code often requires a final polish, it provides a massive head start. This guide shows the 10 best ggplot2 boxplot prompts, each paired with complete code, and a brief discussion on what makes a good prompt, covering simple, grouped, notched, violin-box plots, and more.

The Art of the Prompt: Getting the Best Code from AI

The key to success is prompt engineering. An effective prompt is:

  • Specific: Name the dataset, variables, and functions (use palmerpenguins, y = body_mass_g, use theme_classic()).
  • Comprehensive: Include details about aesthetics (colors, themes, fonts), labels, legends, and final output (file type, DPI).

    Logical: For complex plots, break the request into layers or steps (“Overlay jittered points,” “First compute n, then add labels”).

This guide presents the 10 best prompts that follow these principles. For each, we’ll analyze why the prompt works so well,

1. ChatGPT Prompt for Simple Boxplot

Here is the ChatGPT prompt for making a simple boxplot where we specify the dataset and variables on x and y axis. In addition we specify them and color palette. And finally to save the plot as publication quality image.

Prompt:

  • Use the palmerpenguins dataset
  • Boxplot of body_mass_g (y) by species (x)
  • Apply a clean theme; bold title; remove minor gridlines
  • Use a color-blind friendly palette; suppress legend
  • Save as PNG (300 dpi, 6.3×4.5 in)

Here’s code generated using ChatGPT (GPT-5)”


library(tidyverse)
library(palmerpenguins)

# Filter out missing values
df %
  drop_na(species, body_mass_g)

# Create the plot
p 


And this is how the boxplot looks.
ggplot2 boxplot made from ChatGPT prompt's code
ggplot2 boxplot made from ChatGPT prompt's code

This ChatGPT prompt succeeds because of its specificity. It doesn't just ask for a boxplot; it explicitly names the dataset, the exact variables for the x and y axes, the desired theme (theme_classic()), and even granular details like making the title bold. This level of detail minimizes ambiguity and leads directly to the desired output.

2. ChatGPT Prompt for Horizontal Boxplot

We use the same template to specify our prompts to create R code to make other boxplot. In this case, the prompt for horizontal boxplot. Flipping the coordinates is a simple yet effective way to handle long category labels.

Prompt:

  • Use palmerpenguins
  • Horizontal boxplot of flipper_length_mm by species using coord_flip()
  • Pastel1 palette; clean theme; bold title
  • Save as PNG (300 dpi, 6.3×4.5 in)

And the code from ChatGPT.


library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, flipper_length_mm)

# Create horizontal boxplot
p 


ggplot2 horizontal boxplot made from ChatGPT prompt's code
ggplot2 horizontal boxplot made from ChatGPT prompt's code

A key reason the ChatGPT prompt works is mentioning the specific ggplot2 function: coord_flip(). While you could just ask for a "horizontal boxplot," specifying the function ensures the AI uses the standard, accepted method, making the code more readable and robust.

3. ChatGPT Prompt for Grouped Boxplot

Grouped boxplots are essential for visualizing interactions between two categorical variables.

Prompt:

  • Use palmerpenguins
  • Boxplot of body_mass_g by species, grouped by sex (dodged)
  • Use Set1 palette; minimal theme; bold title
  • Save as PNG (300 dpi, 6.3×4.5 in)

library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, sex, body_mass_g)

# Grouped (dodged) boxplot
p 


ggplot2 grouped boxplot made from code generated by ChatGPT prompt
ggplot2 grouped boxplot made from code generated by ChatGPT prompt

This prompt is effective because it uses precise terminology. Words like "grouped by sex" and "dodged" directly map to ggplot2 concepts (the fill aesthetic and position_dodge()), guiding the AI to generate the correct code for side-by-side comparisons.

4. ChatGPT Prompt for Boxplot with jittered points

One of my favorites as it shows raw data points and it can reveal sample size and distribution nuances.

Prompt:

  • Use palmerpenguins
  • Boxplot of body_mass_g by species
  • Suppress boxplot outliers; overlay jittered points (width 0.2, alpha 0.5)
  • Set3 palette; no legend; minimal theme; bold title
  • Save as PNG (300 dpi, 6.3×4.5 in)

library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, body_mass_g)

# Boxplot + jittered points
p 


ggplot2 boxplot with jitter made from code generated by ChatGPT prompt
ggplot2 boxplot with jitter made from code generated by ChatGPT prompt

A big reason this prompt works is detailed instructions. Notice the two-part instruction: "Suppress the default outliers" and "Overlay jittered data points." This step-by-step logic is crucial for creating layered plots. The prompt also provides specific parameters (width = 0.2, alpha = 0.5), which translates directly into function arguments in the geom_jitter() call

5. ChatGPT Prompt for Notched Boxplot

A notched boxplot adds a confidence interval around the median.

Prompt:

  • Use palmerpenguins
  • Notched boxplots of body_mass_g by species
  • Dark2 palette; legend off; clean theme; bold title
  • Save as PNG (300 dpi, 6.3×4.5 in)


library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, body_mass_g)

# Notched boxplots
p 


ggplot2 Notched Boxplot made from code generated by ChatGPT prompt
ggplot2 Notched Boxplot made from code generated by ChatGPT prompt

The prompt uses a highly relevant term "notched boxplots," and that leverages the AI's knowledge of standard ggplot2 terminology, which correctly translates to setting notch = TRUE within the geom_boxplot() function

6. ChatGPT Prompt to Annotate n on Boxplot

Displaying the sample size (n) for each group is crucial for proper interpretation and to trust the plot.

Prompt:

  • Use palmerpenguins
  • Boxplot body_mass_g by species; compute n per species and label above boxes
  • Leave headroom with coord_cartesian()
  • Pastel2 palette; clean theme; bold title
  • Save as PNG (300 dpi, 6.8×4.8 in)

library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, body_mass_g)

# Compute sample sizes per species
n_labels %
  group_by(species) %>%
  summarise(n = n(), .groups = "drop") %>%
  mutate(y = max(df$body_mass_g, na.rm = TRUE) + 200)  # position labels above boxes

# Boxplot with sample size labels
p 


ggplot2 boxplot with sample size: code generated by ChatGPT prompt
ggplot2 boxplot with sample size: code generated by ChatGPT prompt

This is slightly advanced prompt that works by breaking the problem into multiple, sequential steps: "Compute the sample size" and then "Add a text label." This guides the AI to first create a summary data frame for the counts and then use geom_text() to add them to the plot, a common and effective pattern in ggplot2

7. ChatGPT Prompt for Violin with Box Overlay

Combines distribution shape with summary stats.

Prompt:

  • Use palmerpenguins
  • Violin of body_mass_g by species (trim = FALSE)
  • Overlay a thin boxplot (width = 0.12, outlier.shape = NA, fill = "white")
  • Set2 palette; clean theme; bold title
  • Save as PNG (300 dpi, 6.8×4.8 in)

library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, body_mass_g)

# Violin with thin box overlay
p 


ggplot2 Violinplot overlayed with Box made from code generated by ChatGPT prompt
overlayed with Box made from code generated by ChatGPT prompt

The prompt is another example of how clear description can be of great help. First, tt asks for a "violin plot" and then to "Overlay a thin boxplot." In addition, it provides very specific aesthetic details for the overlay (width = 0.12, fill = "white") gives the AI all the information it needs to construct the complex visual correctly.

9. ChatGPT Prompt to Reorder Boxplot by Median

Prompt:

  • Use palmerpenguins
  • Reorder x by median of y: reorder(species, body_mass_g, median).
  • Draw boxplots width 0.6; outliers semi-transparent; palette Set2; legend off.
  • Clean theme; bold title; remove minor gridlines
  • Export: PNG 300-dpi (6.5×4.5 in)

library(tidyverse)
library(palmerpenguins)

# Drop missing values
df % drop_na(species, body_mass_g)

# Reorder species by median body mass
p 

ggplot2 boxplot ordered by Median made from code generated by ChatGPT prompt
ggplot2 boxplot ordered by Median made from code generated by ChatGPT prompt

This prompt is highly effective because it explicitly describes the desired outcome: "Reorder the species on the x-axis by the median." Also, this is a common enough task that the AI knows to use the reorder() function within the aes() mapping, which is the idiomatic ggplot2 solution.

10. ChatGPT Prompt for Notched Boxplot

Prompt:

  • Use palmerpenguins.
  • Create quartile bins of bill_length_mm with cut_number(..., n = 4) labeled Q1–Q4.
  • Map x = bill_len_bin, y = body_mass_g; palette Set3; legend off.
  • Clean theme; bold title.
  • Export: PNG 300-dpi (6.6×4.6 in)


library(tidyverse)
library(palmerpenguins)

# Drop missing values and create quartile bins for bill length
df %
  drop_na(bill_length_mm, body_mass_g) %>%
  mutate(bill_len_bin = cut_number(bill_length_mm, n = 4, labels = paste0("Q", 1:4)))

# Boxplots of body mass by bill-length quartile
p 

This is another example where the prompt is very specific/precise on what we need to achieve. To start off it looks complex, but specifying the function cut_number() is key. The prompt not only names the function but also its key arguments (n = 4) and the desired output (labels = Q1–Q4). This precise instruction for the data preprocessing step ensures the foundation of the plot is correct.

ChatGPT failure Example: Boxplot with lines connecting Paired Plot

Coming up with a specific prompt is often difficult. Here is a case where the prompt to create boxplot with lines connecting paired datapoint.

Prompt:

    • Use built-in sleep dataset (convert to tibble); create subject ID per pair
    • Boxplots of extra (y) by group (x)
    • Overlay thin subject lines connecting paired observations (geom_line() with group = ID)
    • Palette Pastel1; clean theme; bold title
    • Save as PNG 300-dpi (6.3×4.5 in)

Our prompt to ChatGPT, generated code that is syntactically correct. However, when run, the result is a plot showing just the boxplots, with the crucial connecting lines mysteriously absent.


library(tidyverse)

# Convert to tibble; create subject ID per pair; keep group as factor for discrete x
sleep_tbl %
  mutate(
    ID    = rep(1:(n()/2), each = 2),   # subject identifier per pair
    group = factor(group, levels = c(1, 2))
  )

# Paired boxplots with thin subject-connection lines
p 

ChatGPT Prompt Specificity Failure: Boxplot with lines connecting paired points
ChatGPT Prompt Specificity Failure: Boxplot with lines connecting paired points

The Diagnosis: Why Did It Fail?

The failure isn't a simple bug; it stems from the prompt's hidden complexities:

Latent Steps: The prompt requires the AI to infer a multi-step process: first, manipulate the data to create an ID column, and second, use the abstract group aesthetic in geom_line() to connect the dots. A misstep in either stage will cause the lines to disappear.

Brittleness of group: The aes(group = ID) mapping is one of the more advanced concepts in ggplot2. It tells the line-drawing geometry which points belong together. It's a powerful tool, but it's not visual and can easily fail if the underlying data isn't structured perfectly.

Missing Key Information: This is the most critical issue. The prompt lacks to add data points to the plot, i.e. geom_point(). Without mentioning raw data points, it is not clear where the lines are supposed to connect. This makes debugging nearly impossible and is a flaw in the visualization's design itself.

Why don't you give a shot at coming up with a prompt that makes boxplots with lines connecting jittered data points.

Explore the Complete ggplot2 Guide

35+ tutorials with code: scatterplots, boxplots, themes, annotations, facets, and more—tested and beginner-friendly.

Visit the ggplot2 Hub → No fluff—just code and visuals.

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: AI for Coding, ChatGPT prompts, Prompt Engineering

Primary Sidebar

Python & R Viz Hubs

  • Seaborn Guide & Cookbook
  • ggplot2 Guide & Cookbook
  • Matplotlib Guide & Cookbook
  • Confusion Matrix Calculator
  • Visualizing Activation Functions
  • Visualizing Dropout
  • Visualizing Loss Functions

Buy Me a Coffee

Copyright © 2026 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version