How To Change X Axis Labels In R
ggplot2 axis ticks : A guide to customize tick marks and labels
- Data
- Instance of plots
- Change the appearance of the centrality tick mark labels
- Hide x and y centrality tick marker labels
- Modify axis lines
- Set axis ticks for detached and continuous axes
- Customize a discrete centrality
- Modify the order of items
- Change tick mark labels
- Choose which items to display
- Customize a continuous centrality
- Set the position of tick marks
- Format the text of tick marker labels
- Customize a discrete centrality
- Infos
The goal of this tutorial is to describe how to customize axis tick marks and labels in R software using ggplot2 package.
Data
ToothGrowth data is used in the examples hereafter.
# Convert dose cavalcade from numeric to factor variable ToothGrowth$dose <- as.factor(ToothGrowth$dose) head(ToothGrowth)
## len supp dose ## 1 four.2 VC 0.five ## 2 11.five VC 0.5 ## three 7.3 VC 0.5 ## 4 v.eight VC 0.5 ## 5 6.four VC 0.five ## half-dozen x.0 VC 0.5
Make certain that dose column are converted every bit a gene using the higher up R script.
Example of plots
library(ggplot2) p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() p
Change the appearance of the centrality tick mark labels
The color, the font size and the font face of axis tick marker labels can be changed using the functions theme() and element_text() as follow :
# x centrality tick mark labels p + theme(axis.text.10= element_text(family unit, face, color, size)) # y axis tick marker labels p + theme(axis.text.y = element_text(family, face up, colour, size))
The following arguments can exist used for the part element_text() to change the appearance of the text :
- family : font family unit
- face : font confront. Possible values are "plain", "italic", "assuming" and "assuming.italic"
- colour : text color
- size : text size in pts
- bending : angle (in [0, 360])
# Change the appearance and the orientation angle # of centrality tick labels p + theme(axis.text.ten = element_text(face="assuming", color="#993333", size=fourteen, angle=45), axis.text.y = element_text(face up="assuming", colour="#993333", size=14, angle=45))
Change axis lines
Centrality lines can exist changed using the role element_line() as follow :
p + theme(axis.line = element_line(color, size, linetype, lineend, color))
The arguments of element_line() are :
- colour, color : line color
- size : line size
- linetype : line blazon. Line type can exist specified using either text ("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash") or number (0, 1, 2, 3, 4, 5, 6). Note that linetype = "solid" is identical to linetype=1. The available line types in R are described in this mail : Line type in R software
- lineend : line end. Allowed values for line end are : "round", "butt" or "square"
# Change the line type and colour of centrality lines p + theme( axis.line = element_line(colour = "darkblue", size = 1, linetype = "solid"))
Set axis ticks for discrete and continuous axes
ten or y axis can be discrete or continuous. In each of these two cases, the functions to exist used for setting axis ticks are different.
Customize a discrete axis
The functions scale_x_discrete() and scale_y_discrete() are used to customize discrete x and y axis, respectively.
It is possible to use these functions to change the following 10 or y centrality parameters :
- axis titles
- centrality limits (data range to display)
- choose where tick marks appear
- manually label tick marks
The simplified formats of scale_x_discrete() and scale_y_discrete() are :
scale_x_discrete(name, breaks, labels, limits) scale_y_discrete(proper name, breaks, labels, limits)
- name : x or y axis labels
- breaks : command the breaks in the guide (axis ticks, grid lines, …). Amidst the possible values, there are :
- NULL : hide all breaks
- waiver() : the default intermission computation
- a character or numeric vector specifying which breaks to brandish
- labels : labels of axis tick marks. Immune values are :
- NULL for no labels
- waiver() for the default labels
- graphic symbol vector to be used for break labels
- limits : a character vector indicating the data range
Note that, in the examples below, we'll apply only the functions scale_x_discrete() and xlim() to customize 10 axis tick marks. The aforementioned kind of examples can be practical to a discrete y axis using the functions scale_y_discrete() and ylim().
Modify the order of items
The argument limits is used to change the order of the items :
# default plot p # Change the order of items # Change the 10 axis name p + scale_x_discrete(name ="Dose (mg)", limits=c("two","1","0.5"))
Change tick marker labels
The name of tick mark texts can be changed as follow :
# Solution i p + scale_x_discrete(breaks=c("0.5","ane","2"), labels=c("Dose 0.5", "Dose i", "Dose 2")) # Solution 2 : same plot equally solution 1 p + scale_x_discrete(labels=c("0.5" = "Dose 0.5", "1" = "Dose 1", "ii" = "Dose 2"))
Cull which items to display
The R code below shows the box plot for the starting time item (dose = 0.five) and the last detail (dose = two) :
# Solution ane p + scale_x_discrete(limits=c("0.5", "ii")) # Solution two : same result as solution 1 p + xlim("0.5", "2")
Customize a continuous centrality
The functions scale_x_continuous() and scale_y_continuous() are used to customize continuous x and y axis, respectively.
Using these two functions, the post-obit 10 or y axis parameters can be modified :
- centrality titles
- axis limits (set the minimum and the maximum)
- choose where tick marks appear
- manually label tick marks
The simplified formats of scale_x_continuous() and scale_y_continuous() are :
scale_x_continuous(name, breaks, labels, limits, trans) scale_y_continuous(proper name, breaks, labels, limits, trans)
- name : x or y axis labels
- breaks : control the breaks in the guide (axis ticks, filigree lines, …). Amongst the possible values, there are :
- NULL : hide all breaks
- waiver() : the default pause computation
- a character or numeric vector specifying the breaks to display
- labels : labels of axis tick marks. Allowed values are :
- Goose egg for no labels
- waiver() for the default labels
- graphic symbol vector to be used for intermission labels
- limits : a numeric vector specifying x or y axis limits (min, max)
- trans for centrality transformations. Possible values are "log2", "log10", "sqrt", etc
These functions can exist used as follow :
# besprinkle plot sp<-ggplot(cars, aes(x = speed, y = dist)) + geom_point() sp # Alter x and y axis labels, and limits sp + scale_x_continuous(proper name="Speed of cars", limits=c(0, xxx)) + scale_y_continuous(name="Stopping altitude", limits=c(0, 150))
Set the position of tick marks
The R code below fix the position of tick marks on the y axis of the box plot. The function scale_y_continuous() and the argument breaks are used to choose where the tick marks appear :
# Set tick marks on y axis # a tick mark is shown on every 5 p + scale_y_continuous(breaks=seq(0,40,5)) # Tick marks can be spaced randomly p + scale_y_continuous(breaks=c(5,7.5, 20, 25)) # Remove tick marker labels and gridlines p + scale_y_continuous(breaks=Zero)
Format the text of tick marking labels
Tick mark labels can exist formatted to exist viewed as percents, dollars or scientific notation. The package scales is required.
library(scales) # Format labels as percents p + scale_y_continuous(labels = percent) # Format labels as scientific p + scale_y_continuous(labels = scientific)
Possible values for labels are comma, per centum, dollar and scientific. For more examples, read the documentation of the package scales : ?scales::trans_new
Infos
This analysis has been performed using R software (ver. 3.1.2) and ggplot2 (ver. )
Enjoyed this commodity? I'd be very grateful if you'd help it spread by emailing information technology to a friend, or sharing it on Twitter, Facebook or Linked In.
Show me some dearest with the similar buttons below... Thank you and please don't forget to share and comment beneath!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en fifty'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.
Montrez-moi un peu d'flirtation avec les similar ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for Yous!
Recommended for y'all
This section contains best information science and cocky-development resources to help you on your path.
Want to Larn More on R Programming and Data Science?
Follow us by Electronic mail
On Social Networks:
Go involved :
Click to follow us on Facebook and Google+ :
Comment this article by clicking on "Discussion" push (height-right position of this page)
Source: http://www.sthda.com/english/wiki/ggplot2-axis-ticks-a-guide-to-customize-tick-marks-and-labels
Posted by: kunkelwhaeld.blogspot.com
0 Response to "How To Change X Axis Labels In R"
Post a Comment