

In ggplot2, we can do this by using expression. This is needed when we want to highlight the title by making it different. In general, the axes titles have simple font but we can change partial or complete title to italics to get the viewers attraction. I hope this helps understand the clumsy execution of complex axis labels.Of course, writing axes titles help viewers to understand the plot in a better way because they add more information to the plot.

By default, the Lattice method shifts text in spanning cells, first to the left and then to the top, as you can observe in the output table above. Steps Create x and y data points using numpy. The asterix is used when we need to write something like “degree” or “pi” to specify a symbol, but when we want it to be next to something, like a C for degree*C. To make subscripts and superscripts, use the '' The tag defines superscript text. The next thing is the use of both “degree” and the asterix _*_. This is because expression does not appreciate anything starting with a number. In the title line, (note that I had to place the title on a separate line…ggplot is precious sometimes) we see quotations around the 20. Anything placed after will be placed above the preceeding character.

Similarly, we specify superscript by using the caret ^ to denote power. In this guide we are going to use the following example plot. You can add some annotations to some coordinates or label data points. Anything inside these will be placed below the preceeding character. The geomtext and geomlabel functions allows adding text or labels, respectively, to plots created with ggplot2. Raw superscript in R ggplot2 axis label. In our x axis, we specify a subscript (lower) by using square brackets. This example, while nonsensical, demonstrates some of the major quirks with the expression() argument/command.Īcross all of the expression arguments, we specify a space between characters/words by using a tilde ~. Raw superscript in R ggplot2 axis label. Labs(title = expression(Sepal~by~Petal~at~"20"*degree*C)) Labs(x = expression(Sepal~Length), y = expression(Petal~Length^cm))+ ggplot(DF,aes(X, Y))+ geompoint(size 8, fill 'green', color 'black', shape 21)+ xlab(bquote(X-Axissuperscript))+ ylab(bquote(Y-Axissuperscript)) Output: ScatterPlot with Superscripted Axis Labels Adding Subscript Axis Labels We will change the label of X to X-Axissubscript and Y to Y-Axissubscript. iris.scatter <- ggplot(iris, aes(x=Sepal.Length, y=Petal.Length, colour=Species)) + These are a few of my own, so they do not make any sense with the given graph. I have written a x-axis label that does not make sense, in an effort to display the most common issues.
How to make a superscript in text in ggplot2 code#
The following code uses the expression() argument to solve these issues. To fix this is simple, but clumsy in how its executed. iris.scatter <- iris.scatter + labs(title= "Relationship between petal and sepal length")Īfter trying to use these labs() commands you will start to realise it hates anything slightly symbolic (subscript, superscript, degrees etc.). If we wish to add a title to our plot (not overly common in publications) we can use the following. iris.scatter <- iris.scatter + labs(x = "Sepal Length (cm)", y = "Petal Length (cm)") If we want to change the axis labels themselves, this is done using the labs() command. Built with the "Learn" Theme using Hugo and Blogdown
