Comment personnaliser l'axe X avec annotation_custom dans ggplot2
Dans mes données réelles, je voudrais supprimer le texte de l'axe des x et ajouter un nouveau texte.
En suivant un exemple d' ici (que je peux reproduire), je cherche à passer une liste de grobs annotation_custommais je n'y parviens pas. Pour être clair, je sais que c'est un moyen beaucoup plus simple de faire pivoter le axix.text.xet ce n'est pas mon objectif et cet exercice peut sembler ridicule pour certains. Mais j'apprécierais de savoir que je fais mal.
#create a dataframe to pass in textGrob
mpg_grob <- c("10", "15", "20", "25", "30", "35")
mpg_axis <- c(10,15, 20, 25, 30, 35)
wt_axis <- rep(-0.07, 6)
dfwow <- data.frame(mpg_grob, mpg_axis, wt_axis)
#create a basic plot
p <- ggplot(mtcars, aes(mpg, wt)) +
facet_grid(.~cyl) +
geom_point() + theme(axis.title.x = element_text(margin=margin(50,0,0,0)), axis.text.x = element_blank()) +
coord_cartesian(clip = "off")
#Add annotations
for (i in 1:length(dfwow$mpg_grob)){ p <- p + annotation_custom( textGrob(label = dfwow$mpg_grob[i],
rot = 90,
gp = gpar(frontsize = 9)),
xmin = dfwow$mpg_axis[i], xmax = dfwow$mpg_axis[i],
ymin = -25,
ymax = -15
)
}
Dans le tracé résultant, il n'y a pas d'annotation.
Si je fais p$layersvoici ce que je vois
> p$layers
[[1]]
geom_point: na.rm = FALSE
stat_identity: na.rm = FALSE
position_identity
[[2]]
geom_custom_ann: grob = list(label = "10", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6711", gp = list(frontsize = 9), vp = NULL), xmin = 10, xmax = 10, ymin = -25, ymax = -15, xmin = 10, xmax = 10, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity
[[3]]
geom_custom_ann: grob = list(label = "15", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6712", gp = list(frontsize = 9), vp = NULL), xmin = 15, xmax = 15, ymin = -25, ymax = -15, xmin = 15, xmax = 15, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity
[[4]]
geom_custom_ann: grob = list(label = "20", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6713", gp = list(frontsize = 9), vp = NULL), xmin = 20, xmax = 20, ymin = -25, ymax = -15, xmin = 20, xmax = 20, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity
[[5]]
geom_custom_ann: grob = list(label = "25", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6714", gp = list(frontsize = 9), vp = NULL), xmin = 25, xmax = 25, ymin = -25, ymax = -15, xmin = 25, xmax = 25, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity
[[6]]
geom_custom_ann: grob = list(label = "30", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6715", gp = list(frontsize = 9), vp = NULL), xmin = 30, xmax = 30, ymin = -25, ymax = -15, xmin = 30, xmax = 30, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity
[[7]]
geom_custom_ann: grob = list(label = "35", x = 0.5, y = 0.5, just = "centre", hjust = NULL, vjust = NULL, rot = 90, check.overlap = FALSE, name = "GRID.text.6716", gp = list(frontsize = 9), vp = NULL), xmin = 35, xmax = 35, ymin = -25, ymax = -15, xmin = 35, xmax = 35, ymin = -25, ymax = -15
stat_identity: na.rm = FALSE
position_identity
Réponses
Vos ymin et ymax sont en dehors de la plage de l'axe y. Il doit être dans la plage où vous souhaitez placer les nouvelles étiquettes.
Essaye ça
#create a basic plot
p <- ggplot(mtcars, aes(mpg, wt)) +
facet_grid(.~cyl) +
geom_point() + theme(axis.title.x = element_text(margin=margin(50,0,0,0)), axis.text.x = element_blank()) +
coord_cartesian(clip = "off")
#Add annotations
for (i in 1:length(unique(dfwow$mpg_grob))){ p <- p + annotation_custom( textGrob(label = paste0('n=',dfwow$mpg_grob[i]),
rot = 90,
gp = gpar(fontsize = 8)),
xmin = dfwow$mpg_axis[i], xmax = dfwow$mpg_axis[i],
ymin = .8,
ymax = 1.5
)
}
p