In Ggplot, the future of better data visualization, we may sometimes need to use some coordinate transformation operations, such as to draw a horizontal bar chart or spider chart.
Coord_cartesian (Xlim = null, Ylim = NULL)
Cartesian coordinates: From the point of view Coord_cartesian parameters are relatively simple, x and y data limits
Coord_flip (...)
Horizontal transition coordinates: Swap x and y axes without special parameters
Coord_trans (x = "Identity", y = "identity", limx = null, Limy = NULL)
Coordinate form conversion: including logarithmic conversion, square root conversion, and so on, where x and Y values can be log10,log2 or squal, and the other two parameters are also limited coordinate range
Coord_equal (ratio=1, ...)
Isometric conversion: After using this function, the x and Y coordinates are converted to equal form, where the graph produces a large scale, and the radio can further adjust the zoom ratio (the ratio of x and Y)
Coord_polar (theta = "X", start = 0, direction = 1)
Polar coordinate transformation: can make spider chart or pie chart effect, parameter aspect Theta can choose X or Y, indicate the extension coordinates, start is coordinates start angle, default actually position is 12 o'clock,
The direction represents the direction of the data, 1 is clockwise, and 1 is counterclockwise.
Let's look at some specific examples:
Library (Ggplot2) P<-ggplot (Mtcars,aes (MPG,HP)) +geom_point (color= "Blue", size=2) P+coord_cartesian ()
The Cartesian coordinate transformation is the same as the normal drawing effect.
P+coord_flip ()
The horizontal and vertical coordinates are interchanged, and a bar chart may be more obvious.
Pp<-ggplot (Mtcars,aes (cyl)) +geom_bar (fill= "Blue", width=0.8) pp
Pp+coord_flip ()
P+coord_trans (x= "log2", y= "sqrt")
The logarithmic and square root transformations of the axes are mentioned in the previous scale settings, and the advantage of the Coord_trans is that x and Y can be converted at a time relative to the scale setting.
P+coord_equal ()
Under normal circumstances, if the graphics are directly coord_equal operation, the final effect is generally very poor, because most of the time, X and y data are basically different and the gap may be relatively large,
The relative setting of the ratio parameter is a wise choice.
P+coord_equal (ratio=0.08)
The effect of this picture, relative to the previous one is much better, of course, you can also set the Ritio parameters further to achieve better results.
P+coord_polar (theta= "x")
P+coord_polar (theta= "Y")
It can be seen that the change of the outer ring coordinates of the two graphs is determined by the selection of the theta parameters.
Pp+coord_polar (theta= "x")
Pp+coord_polar (theta= "Y")
The effect of using the bar with the conversion becomes the above appearance, although the graphic is cool, but the visibility is not as good as the bar chart, if the cyl at three levels, the data entry is the same, the result is a pie chart.
This method is a way to implement a pie chart in Ggplot.
Ggplot2 coordinate system Related settings (coord)