Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add subtext to axes in ggplot2 R

For the main y-axis and x-axis, I have generic titles like "Tank's Ratio" and "Counts". I want a second line of label where I specify the ratio and counts. eg. Just below "Tank's Ratio" I want "# in water/# in sand" in a smaller font but along the y-axis. Similarly for the x-axis. Here is the basic code

data <- data.frame(set = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 3, 4, 4), density = c(1, 3, 3, 1, 3, 1, 1, 1, 3, 3, 1, 3), counts = c(100, 2, 3, 76, 33, 12, 44, 13, 54, 36, 65, 1), ratio = c(1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 90, 1))

library(ggplot2)

ggplot(data, aes(x = counts, y = ratio)) + 
  geom_point() + 
  ylab("Tank's Ratio") + 
  xlab("Counts") 
like image 518
Biotechgeek Avatar asked Jan 08 '19 13:01

Biotechgeek


People also ask

How do I add axis labels in R?

To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.

How do you specify axes in ggplot2?

Setting the axis bounds on a plot using ggplot2 is a common task. Using the following functions, you can accomplish so quickly. xlim(): specifies the lower and upper limit of the x-axis. ylim(): specifies the lower and upper limit of the y-axis.

How do you customize axes in R?

You can create custom axes using the axis( ) function. axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...) the coordinate at which the axis line is to be drawn. If you are going to create a custom axis, you should suppress the axis automatically generated by your high level plotting function.


2 Answers

It's not the most elegant solution, but hope it helps:

library(ggplot2)
library(gridExtra)
library(grid)

First, create plot without ylab:

g <- ggplot(data, aes(x = counts, y = ratio)) + 
  geom_point()  +
  ylab("") + 
  xlab("Counts") 

Then add subtitle for both axis:

g2 <- grid.arrange(g, 
                   bottom = textGrob("in water/ # in sand", 
                                     x = 0.55, y = 1, gp = gpar(fontsize = 9)),
                   left = textGrob("in water/ # in sand",  rot = 90, 
                                   x = 1.5, gp = gpar(fontsize = 9)))

And finally, add description of y-axis

grid.arrange(g2, 
             left = textGrob("Tank's Ratio",  rot = 90, 
                             x = 1.7, gp = gpar(fontsize = 12)))

enter image description here

like image 103
Adela Avatar answered Sep 20 '22 02:09

Adela


You can add x and main titles.

EDIT: This is ridiculously slooow!

  #library(extrafont)
#loadfonts(dev="win")
library(tidyverse)
data %>%
ggplot(aes(x=counts, y=ratio)) + geom_point() +
labs(y=expression(atop(bold("Tank's Ratio"),atop(italic("#in water #in sand")))))+
  theme_minimal()+
  theme(axis.title.y = element_text(size=15,family="Comic Sans MS"))

enter image description here

ORIGINAL:

library(tidyverse) 

      data %>%
    ggplot(aes(x=counts, y=ratio)) + geom_point() +
    labs(y="Tank's Ratio \n #in Water#in sand")
like image 24
NelsonGon Avatar answered Sep 19 '22 02:09

NelsonGon