Bird
Raised Fist0
R Programmingprogramming~20 mins

Saving plots (ggsave) in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ggsave Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output file type saved by this ggsave command?
Consider the following R code that creates a plot and saves it using ggsave. What file type will be created?
R Programming
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave(filename = "myplot.png", plot = p)
AA PDF file named 'myplot.png' is saved
BA PNG file named 'myplot.png' is saved
CA JPEG file named 'myplot.png' is saved
DNo file is saved because the extension is incorrect
Attempts:
2 left
💡 Hint
Check the file extension in the filename argument.
❓ Predict Output
intermediate
2:00remaining
What will be the dimensions of the saved plot?
What are the width and height in inches of the saved plot from this code?
R Programming
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave(filename = "plot.pdf", plot = p, width = 5, height = 4)
AWidth 5 inches, Height 4 inches
BWidth 4 inches, Height 5 inches
CWidth and height default to 7 inches each
DWidth and height are ignored and plot size depends on screen size
Attempts:
2 left
💡 Hint
Look at the width and height arguments in ggsave.
❓ Predict Output
advanced
2:00remaining
What error does this ggsave command produce?
What error will this code produce when run?
R Programming
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave(filename = "plot", plot = p)
AError: Unknown graphics device 'plot'
BNo error, saves a file named 'plot' with default format
CError: No file extension provided in filename
DError: plot object is missing
Attempts:
2 left
💡 Hint
ggsave needs a file extension to know the format.
❓ Predict Output
advanced
2:00remaining
What is the resolution (dpi) of the saved image?
What is the dpi (dots per inch) of the saved plot from this code?
R Programming
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave(filename = "plot.png", plot = p, dpi = 300)
A300 dpi
B150 dpi
C72 dpi
DDefault dpi depends on device and is not set here
Attempts:
2 left
💡 Hint
Check the dpi argument in ggsave.
❓ Predict Output
expert
3:00remaining
What is the output of this code saving multiple plots in a loop?
What files will be created and what will be their contents after running this code?
R Programming
library(ggplot2)
for(i in 1:3) {
  p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + ggtitle(paste("Plot", i))
  ggsave(filename = paste0("plot", i, ".png"), plot = p, width = 4, height = 3)
}
ANo files are saved because ggsave cannot be used inside a loop
BOne PNG file named plot3.png with the last plot titled 'Plot 3'
CThree PNG files named plot1.png, plot2.png, plot3.png but all plots have the title 'Plot 3'
DThree PNG files named plot1.png, plot2.png, plot3.png each with a scatter plot titled 'Plot 1', 'Plot 2', and 'Plot 3' respectively
Attempts:
2 left
💡 Hint
Look at the filename and title inside the loop.

Practice

(1/5)
1. What does the ggsave() function do in R's ggplot2 package?
easy
A. Saves the current or specified plot as an image file
B. Displays the plot on the screen
C. Creates a new plot object
D. Deletes the last plot created

Solution

  1. Step 1: Understand the purpose of ggsave()

    The ggsave() function is designed to save plots created by ggplot2 as image files like PNG, PDF, or JPEG.
  2. Step 2: Differentiate from other plot functions

    Unlike functions that display or create plots, ggsave() specifically saves the plot to a file on disk.
  3. Final Answer:

    Saves the current or specified plot as an image file -> Option A
  4. Quick Check:

    ggsave() = Save plot [OK]
Hint: Remember: ggsave saves plots to files, not just shows them [OK]
Common Mistakes:
  • Thinking ggsave displays the plot
  • Confusing ggsave with plot creation
  • Assuming ggsave deletes plots
2. Which of the following is the correct syntax to save the last plot as a PNG file named "plot.png" with default size using ggsave()?
easy
A. ggsave("plot.png", plot)
B. ggsave(plot.png)
C. ggsave("plot.png")
D. ggsave(plot, "plot.png")

Solution

  1. Step 1: Check the basic ggsave syntax

    The simplest way to save the last plot is to call ggsave() with the filename as a string, e.g., ggsave("plot.png").
  2. Step 2: Identify incorrect syntax

    ggsave(plot.png) lacks quotes around filename, causing an error. Options A and D reference an undefined plot or have wrong argument order.
  3. Final Answer:

    ggsave("plot.png") -> Option C
  4. Quick Check:

    Filename as string = ggsave("plot.png") [OK]
Hint: Filename must be a quoted string in ggsave() [OK]
Common Mistakes:
  • Forgetting quotes around filename
  • Passing plot object without naming argument
  • Swapping argument order
3. What will be the result of this code?
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave("myplot.pdf", plot = p, width = 5, height = 4)
medium
A. Saves the plot 'p' as a 5x4 inch PDF file named 'myplot.pdf'
B. Throws an error because width and height are missing units
C. Saves the last plot instead of 'p' ignoring the plot argument
D. Creates a PNG file instead of PDF

Solution

  1. Step 1: Analyze the ggsave parameters

    The code specifies filename "myplot.pdf", plot object p, and size 5 by 4 inches. This is valid syntax.
  2. Step 2: Understand default units and file type

    Width and height default to inches if units not specified. The file extension '.pdf' sets the output format to PDF.
  3. Final Answer:

    Saves the plot 'p' as a 5x4 inch PDF file named 'myplot.pdf' -> Option A
  4. Quick Check:

    ggsave() saves plot with given size and format [OK]
Hint: File extension sets format; width/height default to inches [OK]
Common Mistakes:
  • Assuming width/height need units explicitly
  • Thinking plot argument is ignored
  • Confusing file format with extension
4. Identify the error in this code snippet:
library(ggplot2)
p <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggsave(filename = myplot.png, plot = p)
medium
A. Missing library call for ggsave
B. Plot object 'p' is not defined
C. ggsave() cannot save PNG files
D. Missing quotes around filename 'myplot.png'

Solution

  1. Step 1: Check the filename argument

    The filename argument must be a string in quotes. Here, myplot.png is unquoted, causing an error.
  2. Step 2: Verify other parts

    The plot object p is defined, PNG is supported, and library(ggplot2) is loaded, so no other errors.
  3. Final Answer:

    Missing quotes around filename 'myplot.png' -> Option D
  4. Quick Check:

    Filename must be quoted string [OK]
Hint: Always put quotes around filenames in ggsave() [OK]
Common Mistakes:
  • Forgetting quotes around filename
  • Assuming ggsave needs extra libraries
  • Thinking PNG format is unsupported
5. You want to save two different ggplot objects, p1 and p2, as PNG files with different sizes. Which code correctly saves p1 as 6x4 inches and p2 as 8x6 inches?
hard
A. ggsave(p1, "p1.png", width = 6, height = 4) ggsave(p2, "p2.png", width = 8, height = 6)
B. ggsave("p1.png", plot = p1, width = 6, height = 4) ggsave("p2.png", plot = p2, width = 8, height = 6)
C. ggsave("p1.png", p1, 6, 4) ggsave("p2.png", p2, 8, 6)
D. ggsave("p1.png", width = 6, height = 4) ggsave("p2.png", width = 8, height = 6)

Solution

  1. Step 1: Check correct argument order and naming

    ggsave("p1.png", plot = p1, width = 6, height = 4) ggsave("p2.png", plot = p2, width = 8, height = 6) uses correct syntax: filename as first argument (string), plot named with plot=, and width/height named.
  2. Step 2: Identify errors in other options

    ggsave(p1, "p1.png", width = 6, height = 4) ggsave(p2, "p2.png", width = 8, height = 6) swaps plot and filename positionally without naming plot=, causing plot object to be assigned to filename (error). ggsave("p1.png", p1, 6, 4) ggsave("p2.png", p2, 8, 6) passes sizes positionally to wrong parameters (device/path). ggsave("p1.png", width = 6, height = 4) ggsave("p2.png", width = 8, height = 6) omits plot argument, so last plot saved instead.
  3. Final Answer:

    Correctly saves both plots with specified sizes -> Option B
  4. Quick Check:

    Filename first, plot named, size named = ggsave("p1.png", plot = p1, width = 6, height = 4) ggsave("p2.png", plot = p2, width = 8, height = 6) [OK]
Hint: Always name plot= and specify filename as first argument [OK]
Common Mistakes:
  • Swapping filename and plot arguments
  • Not naming plot argument
  • Omitting plot argument saves last plot instead
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard