Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Themes and Exporting Plots | Section
Information Visualization with ggplot2 in R
Sezione 1. Capitolo 10
single

single

bookThemes and Exporting Plots

Scorri per mostrare il menu

After you have learned how to build and customize a wide range of plots in ggplot2, you may want to ensure your visualizations have a consistent, professional look and are ready to be shared outside of R. ggplot2 offers powerful tools for styling your plots with themes, as well as functions for exporting your work to image files. A theme in ggplot2 controls the non-data elements of your plot, such as background color, grid lines, font, and legend appearance. ggplot2 provides several built-in themes like theme_minimal(), theme_classic(), and theme_dark(), each offering a distinct style. You can apply these themes directly to your plot, and further customize specific elements using the theme() function. Customizations might include changing the font size of axis titles, adjusting legend placement, or modifying background colors. By combining built-in themes with targeted adjustments, you can create plots that match your desired aesthetic or publication requirements.

123456789101112131415
library(ggplot2) # Create a basic scatter plot p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point(color = "steelblue", size = 3) + labs(title = "MPG vs Weight", x = "Weight", y = "Miles per Gallon") # Apply a built-in theme and customize text p + theme_minimal() + theme( plot.title = element_text(size = 18, face = "bold", color = "navy"), axis.title = element_text(size = 14), axis.text = element_text(color = "gray20") )
copy

Once your plot looks the way you want, you can export it for use in presentations, reports, or publications. The ggsave() function makes it easy to save your current plot to a file. By default, ggsave() will save the last plot you displayed, but you can also specify which plot to save by passing it as the first argument. You can set the filename, file type (such as PNG, JPEG, PDF, or SVG), and image dimensions. For example, to save a plot as a PNG file, you might use ggsave("myplot.png"). Adjust the width and height arguments to control the size of the exported image. This allows you to share high-quality graphics with collaborators or include them in documents and slides.

Compito

Scorri per iniziare a programmare

Apply a theme to a scatter plot and customize its text elements.

  • Load the ggplot2 package.
  • The base scatter plot my_plot is already created for you.
  • Apply the built-in theme_classic() function to style the plot.
  • Customize the plot title and axis titles using the theme() function.
  • Utilize element_text() to set the plot.title to size 16, bold face, and "darkred" color.
  • Utilize element_text() to set the axis.title to size 12 and "black" color.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 10
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt