ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl)))
# Save the last printed plot
ggsave(filename = "my_plot_1.pdf")
# Save the plot to a variable first
plot <- ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point(aes(color = factor(cyl)))
ggsave(filename = "my_plot_1.png", plot)