Introduction
We can also handle the axes of the graph, including x, y-axis swapping, setting axis range, tick mark modification and removal, and so on. In order to play the graphics, axis processing proficiency is indispensable. Axis Swap
We use the Coord_flip () function to swap the axes.
Library (GGPLOT2)
Library (Gcookbook)
Ggplot (Plantgrowth, AES (X=group, Y=weight)) + Geom_boxplot ()
Ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot () + Coord_flip ()
Replacing the x and Y axes sometimes causes the coordinates of an axis to be reversed. For example, the original x-axis data is read from left to right, after the swap, the original data will be presented from the bottom up. Sometimes this is a problem. If x is a factor, we can use the Limits=rev (levels (...)) with the parameter. The function Scale_x_discrete () is modified.
If it is a continuous variable, we can use the function scale_y_reverse or scale_x_reverse to reverse the axis.
If we want to change the order in which the category variables are displayed, you can set the Scale_x_discrete () or Scale_y_discrete () function on the line.
#因子坐标轴
Ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot () + coord_flip () +
Scale_x_discrete (limits= Rev (Levels (Plantgrowth$group)))
#连续变量的逆转
ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot () + Scale_y_reverse ()
#改变分类变量顺序
ggplot (Plantgrowth, AES (X=group, Y=weight)) + Geom_boxplot () +scale_x_discrete ( Limits=c ("Trt1", "Ctrl", "Trt2"))
Set the range of continuous variables for the x,y axis
We can use Xlim () and Ylim to set the maximum minimum value for the X and Y axes.
P <-Ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot ()
p + ylim (0, Max (plantgrowth$weight))
#ylim () is a quick implementation of the scale_y_continuous ().
#其中breaks用来控制刻度.
p + scale_y_continuous (limits=c (0), breaks=null)
Set x, Y axis scale relative proportions
We can set the relative proportions of the x and Y axes scale, which can be 1:1, that is, the consistent length. You can also make the scale of one axis become longer, or the distance between the two tick marks is greater.
SP <-Ggplot (Marathon, AES (X=half,y=full)) + geom_point ()
#1:1
sp + coord_fixed () +
scale_y_continuous ( Breaks=seq (0, 420)) +
scale_x_continuous (breaks=seq (0, 420,)
#1:2 x-axis scale length than y-axis scale length
SP + coord_fixed ( RATIO=1/2) +
scale_y_continuous (breaks=seq (0, 420,)) +
scale_x_continuous (breaks=seq (0, 420, 15))
Set the position of tick marks
The Ggplot () function is good at working with tick marks, as long as you change the spacing of the ticks (the tick marks here refer to the large tick marks).
If the variable on the axis is a continuous variable, there is no small tick mark.
#连续变量
Ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot () +
scale_y_continuous (Breaks=c (4, 4.25, 4.5 , 5, 6, 8)
#看到没有, the Scale_x_discrete function is used when the tick mark position has changed #如果是离散变量 according to the parameters set by breaks
#通过limit函数来控制标签的个数和顺序
Ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot () +
Scale_x_discrete (limits=c ("Trt2", "Ctrl"))
Remove tick marks, scale labels, and gridlines
If you want to get rid of the tick marks, we use theme (Axis.text.y = Element_blank ()) (If you change the x to x, the parameter y is changed to X.).
If you want to remove tick marks, use axis.ticks.y = Element_blank () in the theme function, if the x, y axes want to remove Axis.ticks.y to axis.ticks.
Sometimes the grid lines make the whole picture seem complicated, and we can also remove it. The breaks parameter value is set to null on the line.
P <-Ggplot (Plantgrowth, AES (X=group, Y=weight)) + Geom_boxplot ()
#去除刻度标签
p + theme (Axis.text.y = Element_ Blank ())
#去除刻度线
p + theme (Axis.ticks.y = Element_blank (), Axis.text.y = Element_blank ())
#去除网格线
p + Scale_y_continuous (Breaks=null)
Change the content and display effect of tick labels
Not only can we change the position of the tick labels, we can also modify the contents of the labels.
HWP <-Ggplot (Heightweight, AES (X=ageyear, Y=heightin)) +
geom_point ()
HWP + scale_y_continuous (breaks=c (
labels=c ("Tiny", "Really\nshort", "short",
"Medium", "Tallish")
#值得注意的是, if the label is too long, you can use \ n Place text on the next line to display
To change the display of tick labels, we can set the value of angle in the Element_text parameter in the theme function.
bp <-Ggplot (Plantgrowth, AES (X=group, Y=weight)) + geom_boxplot () + Scale_x_discrete (
breaks=c ("Ctrl", "Trt1", " Trt2 "),
labels=c (" Control "," Treatment 1 "," Treatment 2 ")
#垂直显示
bp + theme (axis.text.x = Element_text ( Angle=90, Hjust=1, vjust=.5))
#换个角度显示, angle is the angle that controls the display of tick marks
bp + theme (axis.text.x = Element_text (angle=30, Hjust =1, vjust=1)
#hjust和vjust是为了控制刻度标签在水平方向和垂直方向的位置
Change axis label Contents
Just changed the tick labels, and we sometimes want to change the contents of the labels in each axis.
HWP + Xlab ("Age in Years") + Ylab ("Height in Inches") #hwp在上面例子中已经定义过
#你也可以这么改, both equivalent
HWP + Labs (x = "Age in Years") y = "Height in Inches")
We can also remove each axis label.
hwp+ Theme (Axis.title.x=element_blank ())
Changing the display of axis labels is also convenient.
HWP + Ylab ("height\n (inches)") +
Theme (Axis.title.y=element_text angle=90, face= "Italic", colour= "darkred",
size=14))
#这也同样适用于改变刻度线标签的字体, color size.
Using logarithmic axes
We can also change the axes into logarithmic axes, using scale_x_log10 () scale_y_log10 ().
Library (MASS)
P <-ggplot (Animals, AES (X=body, Y=brain, label=rownames)) +
Animals (geom_text)
#只显示标签线
p + scale_x_log10 () + SCALE_Y_LOG10 ()
The book also describes how to display the date and time on the axis of the method, interested friends can turn over the book to understand.