Challenge - 5 Problems
ggsave Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)Attempts:
2 left
💡 Hint
Check the file extension in the filename argument.
✗ Incorrect
ggsave uses the file extension to determine the output format. Since the filename ends with '.png', it saves a PNG file.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Look at the width and height arguments in ggsave.
✗ Incorrect
The width and height arguments set the size of the saved plot in inches. Here, width=5 and height=4 inches.
❓ Predict Output
advanced2: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)Attempts:
2 left
💡 Hint
ggsave needs a file extension to know the format.
✗ Incorrect
ggsave requires a file extension to determine the output format. Without it, it throws an error about missing extension.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Check the dpi argument in ggsave.
✗ Incorrect
The dpi argument sets the resolution of the saved image. Here it is explicitly set to 300 dpi.
❓ Predict Output
expert3: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) }
Attempts:
2 left
💡 Hint
Look at the filename and title inside the loop.
✗ Incorrect
The loop creates three separate plots with different titles and saves each to a uniquely named file.