Understanding Layers and Syntax
Svep för att visa menyn
The Grammar of Graphics is a powerful framework for building data visualizations, and ggplot2 brings this approach to R. Instead of thinking about plots as fixed charts, you build them by layering components: you start with your data, map variables to visual properties (called aesthetics), and then choose geometric objects to represent the data. This layered structure lets you build complex, customized graphics by combining simple parts.
12345library(ggplot2) # Basic scatter plot with ggplot2 ggplot(data = mtcars, mapping = aes(x = wt, y = mpg)) + geom_point()
In this code, you see how a ggplot2 plot is constructed by adding layers. The first layer specifies the data source with data = mtcars. The mapping = aes(x = wt, y = mpg) part defines the aesthetic mappings, telling ggplot2 which variables to plot on the x and y axes. The geom_point() layer adds points to the plot, representing each observation as a dot. This structure - data, aesthetics, and geometric object - is the foundation of every ggplot2 visualization.
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal