This is based on my previous post in Adding shapes near to the text in ggplot
Based on the accepted answer from @Tim G
library(ggplot2)
library(ggalt)
library(ggpp)
library(grid)
df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
ggplot(df, aes(y = trt, x = l, xend = r)) +
geom_dumbbell(size = 3, color = "#e3e2e1",
colour_x = "#5b8124", colour_xend = "#bad744") +
geom_text_npc(aes(npcx = 'center', npcy = 'bottom'), label = expression('No. of'~italic('AA')~'isolates with corresponding types'), parse = TRUE) +
annotation_custom(
grob = rectGrob(
x = unit(0.5, "npc"), y = unit(0.04, "npc"),
width = unit(0.4, "npc"), height = unit(0.011, "npc"),
gp = gpar(fill = "gray60", col = NA)
)
)
I wonder if it possible to push the expression generated by geom_text_npc()
(along with the underline created) further below just little above the bottom edge of the plot, however without modifying the y-axis
. I tried with passing nudge_y = -1
in geom_text_npc()
, but it didnt work.
Thanks for your time and attention.
You can give npcy
as exact number in your geom_text_npc(aes(npcx = 'center', npcy = 0.02)
and in annotation_custom
adjust y = unit(0.04, "npc")
to y = unit(0.01, "npc")
.
As pointed out correctly by @Edward, if you don't want the warning, pass a data argument to geom_text_npc()
containing only 1 row (since you have only one label), which is basically annotation since the text label is not connected to your dumbell data.
library(ggplot2)
library(ggalt)
library(ggpp)
library(grid)
df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
ggplot(df, aes(y = trt, x = l, xend = r)) +
geom_dumbbell(size = 3, color = "#e3e2e1",
colour_x = "#5b8124", colour_xend = "#bad744") +
geom_text_npc(data=data.frame(), # pass empty data here
aes(npcx = 'center',
npcy = 0.02), # adjust y-pos of text here
label = expression('No. of'~italic('AA')~'isolates with corresponding types'
), parse = TRUE) +
annotation_custom(
grob = rectGrob(
x = unit(0.5, "npc"), y = unit(0.01, "npc"), # adjust y-pos of the grey bar
width = unit(0.4, "npc"), height = unit(0.011, "npc"),
gp = gpar(fill = "gray60", col = NA)
)
)
Edit While this works, Stefan's answer provides a cleaner / easier to adjust approach and I woould recommend it.