Pour l'axe principal des y et l'axe des x, j'ai des titres génériques comme "Tank's Ratio" et "Counts". Je veux une deuxième ligne d'étiquette où je spécifie le rapport et les comptes. par exemple. Juste en dessous de "Tank's Ratio", je veux "# in water / # in sand" dans une police plus petite mais le long de l'axe y. De même pour l'axe des x. Voici le code de base
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")
3 Réponses :
Ce n'est pas la solution la plus élégante, mais j'espère qu'elle vous aidera:
grid.arrange(g2, left = textGrob("Tank's Ratio", rot = 90, x = 1.7, gp = gpar(fontsize = 12)))
D'abord, créez un tracé sans ylab:
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)))
Ensuite, ajoutez sous-titre pour les deux axes:
g <- ggplot(data, aes(x = counts, y = ratio)) + geom_point() + ylab("") + xlab("Counts")
Et enfin, ajoutez la description de l'axe y
library(ggplot2) library(gridExtra) library(grid)
Vous pouvez ajouter x et les titres principaux.
MODIFIER: C'est ridiculement lent!
library(tidyverse) data %>% ggplot(aes(x=counts, y=ratio)) + geom_point() + labs(y="Tank's Ratio \n #in Water#in sand")
#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"))
Cela n'utilise pas une taille de police plus petite que OP le souhaite.
Edité mais je pense que c'est trop lent.
Vous pouvez utiliser le code suivant, en définissant vous-même les marges, les titres des axes et les sous-titres:
Nous utilisons le thème
pour augmenter la marge inférieure et gauche, et pour supprimer les titres des axes.
Nous utilisons annoter
pour générer le texte qui sert de titre d'axe et de sous-titre, si nécessaire, le texte est tourné.
Nous générer le tracé, le transformer en grob
, et avec ce grob
nous pouvons désactiver le découpage et montrer le tracé.
g1 <- ggplot(data = data, aes(x = counts, y = ratio, group = 1)) + geom_point() + ## increase margin size for left and bottom and ## remove the axis titles theme(plot.margin = unit(c(1, 1, 4, 4), "lines"), axis.title.y = element_blank(), axis.title.x = element_blank() ) + ## define the plotting area to NOT include the annotations coord_cartesian(xlim = c(0, 100), ylim= c(0, 100), expand = FALSE) + ## annotate y axis annotate(geom = "text", x = -9, y = 50, label = "Tank's Ratio", angle = 90, size = 5) + annotate(geom = "text", x = -5, y = 50, label = "#in water/#in sand", angle = 90, size = 4) + ## annotate x axis annotate(geom = "text", x = 50, y = -5, label = "Counts", size = 5) + annotate(geom = "text", x = 50, y = -9, label = "#in water/#in sand", size = 4) ## turn off clipping for axis extra labels g2 <- ggplot_gtable(ggplot_build(g1)) g2$layout$clip[g2$layout$name == "panel"] <- "off" grid::grid.draw(g2)
Je ne suis pas sûr de redimensionner la police, mais vous pouvez ajouter un caractère de nouvelle ligne (\ n) au ylab. Essayez
ggplot (data, aes (x = count, y = ratio)) + geom_point () + ylab ("Tank's Ratio \ n # in water / # in sand") + xlab ("Counts") code >