R ggplot2- language Visualization Toolkit (basic figure type)

Loading required package

library(effects)
library(ggplot2)

Description of the basic graph types

The following geometric objects are ggplot2 essential part of the graph. Each geometry object graphics can build their own independent, but can also be combined to build more complex geometry. These are basically geometric objects associated with a common pattern: When a web graphics using only geometric objects built, this figure tend to have a specific name.

These are two-dimensional geometric objects, so the x and y properties of two kinds of graphics are indispensable. At the same time, they can accept colourand sizegraphic attributes Further, filled geometric objects (bar, tiles ( tile) and polygonal) may also be acceptable fillgraphical properties. Properties using dot shape pattern, line, and path receiving linetypegraphics attributes. These geometric objects can be used to display the raw data, summary data calculated separately and metadata.

  • geom_area()FIG area for drawing (area plot), that is based on the normal line map, the pattern area is filled by the lower y-axis direction. For packet data, each group will be drawn sequentially stacked manner.

  • geom_bar(stat="identity")Draw a bar graph. We need to specify stat="identity", because the default statistics will be automatically converted to "value" to count (so this is essentially a one-dimensional geometric objects). The statistical data transformed identity will remain unchanged.
    By default, the same position a plurality of bar will form sequentially stacked upwardly drawn.

  • geom_line()Draw a line chart. Graphics attribute groupdetermines which observations are connected together;
    Reference R setting ggplot2- visual language, mapping, packet, pattern matching properties and graphic objects
    for more details. geom_pathWith geom_linesimilar, but geom_paththe lines are connected in the order they appear in the data, instead of connecting from left to right.

  • geom_point()Draw a scatter plot.

  • geom_polygon()Drawing a polygon, i.e., the path after filling. Each row of data represents a polygon vertices. Before drawing the polygon apex coordinate data and the original data is often more convenient to merge. Follow-up content to the map data as an example to clarify this concept in detail.

  • geom_text()Tags can be added at the given point. It is the only one of these geometric objects require additional graphical properties: it needs to specify labelparameters. We can set an optional graphical properties hjustand vjustto control the vertical and horizontal position of the text; Furthermore, graphical properties can be provided angleto control the rotation of the text. You may be ?geom_textmore to learn.

  • geom_tile()FIG color depth for drawing (image plot) or FIG horizontal (level plot). All tile (the tile) constitutes a plane slicing a rule and often will be fillmapped graphics attributes to another variable.

The following code draws more geometric objects, the results shown in FIG.

Practice Examples

The basic geometric objects using different drawing effect the same data.

Pattern names are from top to bottom: scatter plots, bar, line, area, the road map containing the label scattergram FIG color depth / level map and polygons FIG.

Observe bar, area, and FIG tile coordinate axis range: three geometric objects occupying the space outside the scope of the data itself, then the axes are automatically stretched.

df <- data.frame(
  x = c(3, 1, 5),
  y = c(2, 4, 6),
  label = c("a","b","c")
)
p <- ggplot(df, aes(x, y)) + xlab(NULL) + ylab(NULL)

Point map + Title

p + geom_point() + labs(title = "geom_point")

Here Insert Picture Description

+ + The title bar of the "value" counts

p + geom_bar(stat="identity") +
    labs(title = "geom_bar(stat=\"identity\")")

Here Insert Picture Description

Line chart title +

p + geom_line() + labs(title = "geom_line")

Here Insert Picture Description

FIG area (in general, based on the diagram, the filled area under the line)

p + geom_area() + labs(title = "geom_area")

Here Insert Picture Description

FIG path (connection data in order of appearance)

p + geom_path() + labs(title = "geom_path")

Here Insert Picture Description

Scattergram containing label (label specified points are added)

p + geom_text(aes(label = label)) + labs(title = "geom_text")

Here Insert Picture Description

FIG color depth or level of FIG.

p + geom_tile() + labs(title = "geom_tile")

Here Insert Picture Description

Polygon drawing, i.e. the path the filled

p + geom_polygon() + labs(title = "geom_polygon")

Here Insert Picture Description

Published 18 original articles · won praise 64 · views 8027

Guess you like

Origin blog.csdn.net/qq_44658157/article/details/105174326