iembry
iembry

Reputation: 944

r - ggplot2 - geom_text - horizontal values above geom_text do not have the same horizontal displacement

I am enclosing a sample dataset via dput and the commented data.table to ask a question about the horizontal displacement of text using geom_text.

library("data.table")
library("ggplot2")

# prevs2

dput(prevs2)
prevs2 <- structure(list(V1 = c("XY02", "XY03", "XY04", "XY16", "XY17"), 
    V2 = c(1L, 15L, 4L, 1L, 1L), N = c(66L, 130L, 55L, 3L, 17L
    ), Prevalence = c(0.0151515151515152, 0.115384615384615, 
    0.0727272727272727, 0.333333333333333, 0.0588235294117647
    ), LCL = c(0.00180338800237566, 0.0804738966923874, 0.0443506970334619, 
    0.28182217271425, 0.0331125475431457), UCL = c(0.0284996423006546, 
    0.150295334076843, 0.101103848421084, 0.384844493952417, 
    0.0845345112803837)), class = c("data.table", "data.frame"
), row.names = c(NA, -5L), .internal.selfref = <pointer: 0x003e2498>)

prevs2 is shown as a data.table below:

#     V1 V2   N Prevalence         LCL        UCL
# 1: XY02  1  66 0.01515152 0.001803388 0.02849964
# 2: XY03 15 130 0.11538462 0.080473897 0.15029533
# 3: XY04  4  55 0.07272727 0.044350697 0.10110385
# 4: XY16  1   3 0.33333333 0.281822173 0.38484449
# 5: XY17  1  17 0.05882353 0.033112548 0.08453451

The following ggplot2 code produces the figure shown below.

ggplot(prevs2, aes(x = reorder(V1, -Prevalence), y = Prevalence)) +
geom_col() + geom_errorbar(aes(ymin = LCL, ymax = UCL), width = 0.1) +    
xlab("V1") + ylab("Prevalence") + geom_point(aes(y = Prevalence)) +
theme(axis.text.x = element_text(size = rel(1.5), angle = 90), 
panel.background = element_rect(fill = "white", colour = "grey50")) + 
theme(plot.margin = unit(c(2, 2, 2, 2), "pt")) + geom_text(aes(label =
N), position = position_dodge(width = 0.9), vjust = -0.9, hjust = -0.9)+ 
theme(text = element_text(size = 12, family = "sans"))

Is there anyway to modify the above ggplot2 code to have equal horizontal displacement from the errorbars for each of the 5 geom_cols in the figure?

If not, is it because the horizontal distance is determined by the length of the text (for example, 130 is farther right than the other values)?

ggplot2 barplot image in .png format

Upvotes: 0

Views: 361

Answers (1)

Marius
Marius

Reputation: 60160

You can use hjust = "left" so the left edge of the text lines up with the errorbar. Then nudge_x to move it away a bit:

ggplot(prevs2, aes(x = reorder(V1, -Prevalence), y = Prevalence)) +
    geom_col() + geom_errorbar(aes(ymin = LCL, ymax = UCL), width = 0.1) +    
    xlab("V1") + ylab("Prevalence") + geom_point(aes(y = Prevalence)) +
    theme(axis.text.x = element_text(size = rel(1.5), angle = 90), 
          panel.background = element_rect(fill = "white", colour = "grey50")) + 
    theme(plot.margin = unit(c(2, 2, 2, 2), "pt")) + 
    geom_text(aes(label = N),  
              vjust = -0.9, hjust = "left", nudge_x = 0.1)+ 
    theme(text = element_text(size = 12, family = "sans"))

I removed position_dodge as I don't think it was actually doing anything, you only have one bar per x-value.

Result:

enter image description here

Upvotes: 1

Related Questions