Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Customizing Scales and Coordinates | section
Hands-On Data Visualization with ggplot2 in R
Abschnitt 1. Kapitel 8
single

single

bookCustomizing Scales and Coordinates

Swipe um das Menü anzuzeigen

Adjusting scales and coordinate systems in ggplot2 allows you to present your data more clearly and highlight important relationships. The scale functions control how data values are mapped to visual properties, such as axis limits or transformations. The coordinate system functions change how data is projected onto the plot, affecting how the viewer interprets patterns and trends. Mastering these tools helps you avoid misleading visualizations and ensures your plots communicate insights effectively.

1234567891011
library(ggplot2) # Basic scatter plot p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() # Setting axis limits p + xlim(2, 5) + ylim(10, 35) # Logarithmic scale for x and y axes p + scale_x_log10() + scale_y_log10()
copy
Note
Note

Using xlim() or ylim() removes data points outside the specified limits before any calculations, which can change summaries or statistical layers.

coord_cartesian() zooms in on a region of the plot without dropping any data, so calculations and summaries remain accurate.

Always use coord_cartesian() to zoom in without altering the underlying data or plot calculations.

Here scale_x_log10() and scale_y_log10() transform the x and y axis to a logarithmic scale, making it easier to visualize data with a wide range of values.

1234567891011
library(ggplot2) # Flipping axes in a boxplot ggplot(mtcars, aes(x = factor(cyl), y = mpg)) + geom_boxplot() + coord_flip() # Creating a polar plot from a bar chart ggplot(mtcars, aes(x = factor(cyl))) + geom_bar() + coord_polar()
copy

coord_flip(): Swaps the x and y axes, turning vertical plots (like boxplots or bar charts) into horizontal ones for better readability.

coord_polar(): Converts a standard plot (like a bar chart) into a circular (polar) layout, useful for creating pie charts or radial bar plots.

Fixed Aspect Ratio

  • coord_fixed() keeps the scale of the x and y axes equal, so one unit on the x-axis is the same length as one unit on the y-axis;
  • This is essential for maps, perfect squares, or any plot where proportions matter;
  • Using coord_fixed() prevents distortion caused by different screen or window sizes.

Aesthetic Scales

You can also control how data is mapped to colors, shapes, or sizes with functions like scale_color_..., scale_shape_..., or scale_size_.... These aesthetic scales help you customize the appearance of your plots and will be explored in the next topic.

12345678
library(ggplot2) # Scatter plot mapping color to 'cyl' and shape to 'gear' ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl), shape = factor(gear))) + geom_point(size = 3) + scale_color_brewer(palette = "Set1") + scale_shape_manual(values = c(16, 17, 15)) + labs(color = "Cylinders", shape = "Gears")
copy

Understanding Arguments for Customizing Plot Aesthetics

  • scale_color_brewer(palette = "Set1"): the palette argument specifies which color palette to use from the ColorBrewer collection. "Set1" is a palette designed for clear, visually distinct categories. This argument is useful when you want to assign meaningful and accessible colors to different groups in your data;
  • scale_shape_manual(values = c(16, 17, 15)): the values argument defines which shapes are used for each category. The numbers correspond to specific plotting symbols in R (16 = solid circle, 17 = solid triangle, 15 = solid square)n;
  • labs(color = "Cylinders", shape = "Gears"): the color and shape arguments set the display names for the plot legend. "Cylinders" will appear above the color legend, and "Gears" above the shape legend. This makes your plot easier to interpret by providing clear, descriptive labels for each mapped aesthetic.
Aufgabe

Wischen, um mit dem Codieren zu beginnen

You have a dataset of diamonds called 'diamonds_small' with two columns: 'clarity' (categorical) and 'price' (numeric). Create a boxplot of price by clarity using ggplot2, then make the following improvements:

  • Transform the y-axis to a logarithmic scale using scale_y_log10();
  • Zoom in on the price range between 1,000 and 5,000 using coord_cartesian(ylim = c(1000, 5000)) so that no data is removed from calculations.

Assign the final plot to the variable p.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 8
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt