0
0
R Programmingprogramming~20 mins

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

Choose your learning style9 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.