How To Add A Title To A Graph In R
Titles and Axes Labels
ggplots are almost entirely customisable. This gives yous the freedom to create a plot design that perfectly matches your report, essay or paper.
This folio provides assist for adding titles, legends and centrality labels.
Earlier you get started, read the page on the basics of plotting with ggplot and install the package ggplot2.
library(ggplot2)
In these examples, let'southward employ a data gear up that is already in R with the length and width of floral parts for iii species of iris. Start, load the information set:
data(iris)
The following lawmaking for a scatter plot of petal length vs sepal length with the three species colour-coded is the base that we volition use throughout this tutorial:
IrisPlot <- ggplot(iris, aes(Sepal.Length, Petal.Length, color = Species)) + geom_point()
Adding a title
To add a title to your plot, add the code +ggtitle("Your Title Hither")
to your line of basic ggplot lawmaking. Ensure you have quotation marks at the outset and end of your title.
impress(IrisPlot + ggtitle("Petal and sepal length of iris"))
If you lot accept a particulary long title that would piece of work better on two lines, use \n
for a new line. Make sure to utilise the correct slash.
print(IrisPlot + ggtitle("Petal and sepal length \nof three species of iris"))
Changing axis labels
To alter the labels on the axis, add the code +labs(y= "y axis name", ten = "x axis name")
to your line of basic ggplot code.
print(IrisPlot + labs(y = "Petal length (cm)", x = "Sepal length (cm)"))
Notation: Yous can too utilise +labs(title = "Championship")
which is equivalent to ggtitle
.
For example:
impress(IrisPlot + labs( title = "Petal and Sepal Length \nof Iris", y = "Petal Length (cm)", x = "Sepal Length (cm)" ))
Changing the legend championship
In the same way y'all edited the title and axis names, you can alter the fable championship past adding +labs(color = "Legend Title")
to the finish of your basic plot lawmaking. Notation: This will just work if you have actually added an extra variable to your bones aes
lawmaking (in this example, using color=Species
to group the points by Species).
IrisPlot <- ggplot(iris, aes(Sepal.Length, Petal.Length, colour = Species)) + geom_point() impress(IrisPlot + labs(colour = "Iris species"))
Note: If you lot are using a histogram, boxplot or bar graph then it is slightly dissimilar. You must apply fill
instead of colour
.
IrisBox <- ggplot(iris, aes(Species, Sepal.Length, fill = Species)) + geom_boxplot() impress(IrisBox + labs(fill up = "Iris species"))
Altering the text fashion of your legend, centrality or championship
The font, colour, size and emphasis of your labels and text tin can all be altered. To do this, utilise the lawmaking theme()
and customise with element_text()
to alter these properties.
The bones format is: mytheme <- theme(championship blazon = element_text(your formats))
Where "title type" specifies which particular text yous want to edit. These can be:
- plot championship. -
plot.title = element_text()
- axis title. -
axis.championship = element_text()
- legend championship. -
legend.title = element_text()
- legend categories -
legend.text = element_text()
- appearance of axis values/numbers. -
axis.text = element_text()
Formatting choices The font, colour, size and accent of whatsoever of these labels can be altered by arguments within element_text(your format)
.
- family. - the font style. Examples of fonts include: "Palatino", "Helvetica", "Courier", "Times". Farther font choices tin can be seen here. For example,
family unit = "Palatino"
- face. - the type of accent, with options including bold, italic and "bold.italic". For case,
face = "assuming.italic"
- color. - the color tin be changed to whatsoever of the colours listed here. Remember to include "" before and after the color name. For example,
colour = "steelblue2"
.
- size. - the size of the text. This is specified by inbound a number. For example,
size = (3)
.
Instance code
Here is an example of a theme that customises the championship, the fable, the axis labels and specifies the font, accent, size and colour of each of these. The figure is then plotted with this theme and further code that provides the content of the championship and axis labels:
mynamestheme <- theme( plot.championship = element_text(family = "Helvetica", face = "assuming", size = (15)), legend.title = element_text(colour = "steelblue", face up = "bold.italic", family = "Helvetica"), legend.text = element_text(face up = "italic", colour = "steelblue4", family = "Helvetica"), axis.title = element_text(family = "Helvetica", size = (10), colour = "steelblue4"), axis.text = element_text(family = "Courier", colour = "cornflowerblue", size = (10)) ) print(IrisPlot + mynamestheme + labs(title = "Petal and sepal \nlength of iris", y = "Petal length (cm)", x = "Sepal length (cm)"))
Removing a label
Another option is to remove the text from the plot entirely. To do this you apply the lawmaking = element_blank()
, remembering those open and closed brackets. The post-obit lawmaking would remove the legend title and axis text.
myblanktheme <- theme( plot.title = element_text(family = "Helvetica", face = "bold", size = (xv)), legend.championship = element_blank(), legend.text = element_text(face = "italic", colour = "steelblue4", family = "Helvetica"), axis.title = element_text(family = "Helvetica", size = (10), color = "steelblue4"), axis.text = element_blank() ) impress(IrisPlot + myblanktheme + labs(title = "Petal and sepal \nlength of iris", y = "Petal length (cm)", ten = "Sepal length (cm)"))
###Further help
To further customise the aesthetics of the graph, including colour and formatting, run across our other ggplot help pages:
* altering overall appearance.
* colours and symbols.
Help on all the ggplot functions can be found at the The primary ggplot assist site.
A useful cheat sheet on unremarkably used functions can exist downloaded hither.
Chang, Westward (2012) R Graphics cookbook. O'Reilly Media. - a guide to ggplot with quite a bit of assist online here
Author: Fiona Robinson
Year: 2016
Last updated: Feb 2022
Source: https://environmentalcomputing.net/graphics/ggplot/ggplot-labels/
0 Response to "How To Add A Title To A Graph In R"
Post a Comment