ChatGPT OpenAI: Hey Teach, Teach me to PLOT with R

© Prof. P.V. (Sundar) Balakrishnan

The Good to Great:

  1. Learning Tool:

Wonderful aspect is that it is not only interactive but patient. However, the queries may need to be well formulated for better answers.

EXAMPLE: Learning to draw a Scatter Plot in R. As an Interactive and Iterative Process:

  1. Show me the commands to create scatter plot graph with ggplot code in R. Use the Diamond dataset for carat and price.

Instructions are clear and annotated.

Output from R for above code:

Looks pretty ugly.

B. How do I make this ggplot better looking

Good teacher is patient

Output from above code:

C. I would like to flip the axes and add color to the points

Great – BUT error in the AXES labels. So need to check carefully!

NEXT CONVERSATION: Try Again, Student!

  1. Show me the commands to create scatter plot with ggplot code in R. Use the diamond dataset for carat and price. place carat size on the Y axis; price on the x-axis. Use color for different carat sizes. Make the plot look very nice.

AXES Labels are now correct.

  1. commands to create scatter plot with ggplot code in R. Use the diamond dataset for carat and price. place carat size on the Y axis; price on the x-axis. Use dramatically colors for the different carat sizes. Format the graph to look very nice.

You can adjust the colors of the points by using a different color scale. For example, you could use the scale_color_viridis_d() function instead of scale_color_viridis_c() to use a more dramatic color palette.

A. flip the axes, add trend line, center and bold the title, make it look nice with a good theme

To flip the axes on the scatter plot, you can swap the x and y arguments in the aes() function used to create the plot. For example, the following code will create a scatter plot with price on the y-axis and carat on the x-axis:

To add a trend line to the scatter plot, you can use the geom_smooth() function. For example, the following code will add a loess trend line to the scatter plot:

To center and bold the title of the scatter plot, you can use the theme() function. For example, the following code will center and bold the title, as well as adjust the size of the axes labels and tick marks:

Finally, to make the scatter plot look nice, you can try using a different theme. The ggplot2 library provides several built-in themes that can be applied to a plot by using the theme_<name>() function.

For example, the following code will apply the theme_bw() theme to the scatter plot, which will give it

# load the ggplot2 library

library(ggplot2)

# load the diamond dataset

data("diamonds")

# create a scatter plot with carat on the x-axis and price on the y-axis

ggplot(data = diamonds, aes(x = carat, y = price)) +

geom_point(aes(color = carat)) +

geom_smooth() +

scale_color_viridis_c(option = "viridis") +

labs(title = "Carat vs Price",

x = "Carat",

y = "Price") +

theme(plot.title = element_text(hjust = 0.5,

face = "bold",

size = 20),

axis.title = element_text(size = 14),

axis.text = element_text(size = 12),

axis.ticks = element_line(size = 1.5))

MAN-MACHINE JOINT EFFORT: R GGPLOT

COMBINED MAN-ChatGPT APPROACH: A lot of hand crafting of the above codes to get the desired effects.

##flip the axes, add trend line, center and bold the title, make the above look nice with a good theme

# load the ggplot2 library

library(ggplot2)

# load the diamond dataset

data(“diamonds”)

# create a scatter plot with carat on the x-axis and price on the y-axis

### COLOR by CUT

ggplot(data = diamonds, aes(x = carat, y = price, color = cut)) +

geom_point(aes(color = cut)) +

geom_smooth() +

scale_color_viridis_d(option = “viridis”) +

labs(title = “Carat vs Price”,

x = “Carat”,

y = “Price”) +

theme(plot.title = element_text(hjust = 0.5,

face = “bold”,

size = 20),

axis.title = element_text(size = 14),

axis.text = element_text(size = 12),

axis.ticks = element_line(size = 1.5))

The UGLY & The Dangerous

Doesn’t Know the Answer and Refuses to Admit. Will give amazing Bullshit of randomly Generated sentences that sound good and even plausible.

Bullshit Generator:

Exactly wrong – with great certitude.

So have fun out there using ChatGPT as an Instructor. But, Beware of the Garbage it puts out.