Using ChatGPT, I produced a publication worthy visualization similar to the one published here: https://fivethirtyeight.com/videos/the-ultimate-halloween-candy-power-ranking/ .
The following are the prompts and resulting code from CGPT:
Prompt 1: Can you write code in R to recreate this chart? *attached screenshot of chart Result 1: A bare bones code structure that built its own data frame from a set of candy names and win percentages.
Prompt 2: Can you recreate the chart using this data? *attached .csv file Result 2: Code that created the data frame from the csv file and a ggplot with only the top 18 ranking candies.
Prompt 3: Can you make the plot interactive? Result 3: Same code as result 2, except using plotly to create the interactive chart instead of using ggplot.
Prompt 4: Can you add their ranking number next to the candy name? Result 4: Same code as result 3, plus a new variable called rank that is displayed next to the candy name in the ploty plot.
Prompt 5: Can you write code that displays the rank, candy name, then win percentage bar within a table? Result 5: A formattable table that displays the candy name, win percentage in a color coded single-bar graph, and rank number.
#Result 1 A bare bones code that created its own data frame for candy_data.
#Result 2
# Load necessary librarieslibrary(ggplot2)
Warning: package 'ggplot2' was built under R version 4.3.3
library(readr)library(dplyr)
Warning: package 'dplyr' was built under R version 4.3.3
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
# Load the datacandy_data <-read_csv("candy-data.csv")
Rows: 85 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): competitorname
dbl (12): chocolate, fruity, caramel, peanutyalmondy, nougat, crispedricewaf...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Select relevant columns and rename for claritycandy_data <- candy_data %>%select(CANDY = competitorname, WIN_PERCENTAGE = winpercent)# Sort data by WIN_PERCENTAGE in descending ordercandy_data <- candy_data %>%arrange(desc(WIN_PERCENTAGE)) %>%head(18) # Take the top 18 candies# Plot the dataggplot(candy_data, aes(x =reorder(CANDY, WIN_PERCENTAGE), y = WIN_PERCENTAGE)) +geom_bar(stat ="identity", fill ="orange") +coord_flip() +labs(title ="Candy Ranking by Win Percentage",x ="Candy",y ="Win Percentage") +theme_minimal() +theme(axis.text.y =element_text(size =8))
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
# Load the datacandy_data <-read_csv("candy-data.csv")
Rows: 85 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): competitorname
dbl (12): chocolate, fruity, caramel, peanutyalmondy, nougat, crispedricewaf...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Select relevant columns and rename for claritycandy_data <- candy_data %>%select(CANDY = competitorname, WIN_PERCENTAGE = winpercent)# Sort data by WIN_PERCENTAGE in descending ordercandy_data <- candy_data %>%arrange(desc(WIN_PERCENTAGE)) %>%head(18) # Take the top 18 candies# Create the ggplot chartp <-ggplot(candy_data, aes(x =reorder(CANDY, WIN_PERCENTAGE), y = WIN_PERCENTAGE, text =paste("Candy:", CANDY, "<br>Win Percentage:", WIN_PERCENTAGE))) +geom_bar(stat ="identity", fill ="orange") +coord_flip() +labs(title ="Candy Ranking by Win Percentage",x ="Candy",y ="Win Percentage") +theme_minimal() +theme(axis.text.y =element_text(size =8))# Convert the ggplot chart to an interactive plotly chartinteractive_plot <-ggplotly(p, tooltip ="text")# Display the interactive chartinteractive_plot
#Result 4
# Sort data by WIN_PERCENTAGE in descending order and add rankingcandy_data <- candy_data %>%arrange(desc(WIN_PERCENTAGE)) %>%head(18) %>%mutate(RANK =row_number(),CANDY_LABEL =paste(RANK, CANDY, sep =". "))# Create the ggplot chartp <-ggplot(candy_data, aes(x =reorder(CANDY_LABEL, WIN_PERCENTAGE), y = WIN_PERCENTAGE, text =paste("Candy:", CANDY, "<br>Win Percentage:", WIN_PERCENTAGE))) +geom_bar(stat ="identity", fill ="orange") +coord_flip() +labs(title ="Candy Ranking by Win Percentage",x ="Candy",y ="Win Percentage") +theme_minimal() +theme(axis.text.y =element_text(size =8))# Convert the ggplot chart to an interactive plotly chartinteractive_plot <-ggplotly(p, tooltip ="text")# Display the interactive chartinteractive_plot
Warning: package 'formattable' was built under R version 4.3.3
Attaching package: 'formattable'
The following object is masked from 'package:plotly':
style
# Sort data by WIN_PERCENTAGE in descending order and add rankingcandy_data <- candy_data %>%arrange(desc(WIN_PERCENTAGE)) %>%head(18) %>%mutate(RANK =row_number())# Create a formattable tableformatted_table <-formattable(candy_data, list(RANK =formatter("span", style =~style(color ="black")),CANDY =formatter("span", style =~style(color ="black")),WIN_PERCENTAGE =formatter("span",style = x ~style(display ="block",direction ="rtl","border-radius"="4px","padding-right"="2px","background-color"=ifelse(x >75, "orange", ifelse(x >50, "lightgreen", "lightblue")),width =paste0(x, "%")), x ~sprintf("%.1f%%", x))))# Display the formatted tableformatted_table
CANDY
WIN_PERCENTAGE
RANK
CANDY_LABEL
Reese’s Peanut Butter cup
84.2%
1
Reese’s Peanut Butter cup
Reese’s Miniatures
81.9%
2
Reese’s Miniatures
Twix
81.6%
3
Twix
Kit Kat
76.8%
4
Kit Kat
Snickers
76.7%
5
Snickers
Reese’s pieces
73.4%
6
Reese’s pieces
Milky Way
73.1%
7
Milky Way
Reese’s stuffed with pieces
72.9%
8
Reese’s stuffed with pieces
Peanut butter M&M’s
71.5%
9
Peanut butter M&M’s
Nestle Butterfinger
70.7%
10
Nestle Butterfinger
Peanut M&Ms
69.5%
11
Peanut M&Ms
3 Musketeers
67.6%
12
3 Musketeers
Starburst
67.0%
13
Starburst
100 Grand
67.0%
14
100 Grand
M&M’s
66.6%
15
M&M’s
Nestle Crunch
66.5%
16
Nestle Crunch
Rolo
65.7%
17
Rolo
Milky Way Simply Caramel
64.4%
18
Milky Way Simply Caramel
:::
:::
#My Tweaked Version
candy_data2 =read_csv("candy-data.csv")
Rows: 85 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): competitorname
dbl (12): chocolate, fruity, caramel, peanutyalmondy, nougat, crispedricewaf...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Select relevant columns and rename for claritycandy_data2 <- candy_data2 %>%select(CANDY = competitorname, WIN_PERCENTAGE = winpercent)# Sort data by WIN_PERCENTAGE in descending order and add rankingcandy_data2 <- candy_data2 %>%arrange(desc(WIN_PERCENTAGE)) %>%mutate(RANK =row_number())# Customize column namescandy_data2 <- candy_data2 %>%rename(`Win Percentage`= WIN_PERCENTAGE)# Create a formattable tableformatted_table <-formattable(candy_data2, list(RANK =formatter("span", style =~style(color ="black")),CANDY =formatter("span", style =~style(color ="black")),`Win Percentage`=formatter("span",style = x ~style(display ="block",direction ="rtl","border-radius"="4px","padding-right"="2px","background-color"=ifelse(x >75, "orange", ifelse(x >50, "lightgreen", "lightblue")),width =paste0(x, "%")), x ~sprintf("%.1f%%", x))))# Add a label to the tablehtml_header <-"<h2 style='text-align: center;'>Halloween Candies Ranked</h2>"html_table <-as.htmlwidget(formatted_table)html_table <- htmltools::tagList(htmltools::HTML(html_header), html_table)# Display the formatted table with the headerhtmltools::browsable(html_table)
Halloween Candies Ranked
#Another Table
candy_data3 =read_csv("candy-data.csv")
Rows: 85 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): competitorname
dbl (12): chocolate, fruity, caramel, peanutyalmondy, nougat, crispedricewaf...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Select relevant columns and rename for claritycandy_data3 <- candy_data3 %>%filter(chocolate ==1| caramel ==1) %>%select(CANDY = competitorname, WIN_PERCENTAGE = winpercent)# Sort data by WIN_PERCENTAGE in descending order and add rankingcandy_data3 <- candy_data3 %>%arrange(desc(WIN_PERCENTAGE)) %>%mutate(RANK =row_number())# Customize column namescandy_data3 <- candy_data3 %>%rename(`Win Percentage`= WIN_PERCENTAGE)# Create a formattable tableformatted_table <-formattable(candy_data3, list(RANK =formatter("span", style =~style(color ="black")),TYPE =formatter("span", style =~style(color ="black")),`Win Percentage`=formatter("span",style = x ~style(display ="block",direction ="rtl","border-radius"="4px","padding-right"="2px","background-color"=ifelse(x >75, "brown", ifelse(x >50, "tan", "beige")),width =paste0(x, "%")), x ~sprintf("%.1f%%", x))))# Add a label to the tablehtml_header <-"<h2 style='text-align: center;'>Halloween Candies Ranked: Chocolate & Caramel Edition</h2>"html_table <-as.htmlwidget(formatted_table)html_table <- htmltools::tagList(htmltools::HTML(html_header), html_table)# Display the formatted table with the headerhtmltools::browsable(html_table)