0

I have seen similar questions and solutions but none as far as I can see relatable to geom_text() in particular. Any guidance is greatly appreciated.

Say I want a plot point estimates and confidence intervals of:

# create tbl

ni <- tribble(
  ~ method, ~ mean_difference, ~ lo95, ~ hi95,
  "NC", 3.235762, -0.5063099, 6.977835,
  "IPTW", 3.256231, -0.5063099, 6.977835,
  "EM", 5.642857, -1.995181, 13.280896,
) 

Next I create a string var pasting together [rounded] mean_difference, lo95, and hi95 — which will be specified as the label for geom_text

# convert to point estimate and confidence intervals to strings (to keep trailing zeros for plot)

to_string <- function(
  var,
  n_digits = 1,
  n_small = 1){
  
  as.character(format(round(var, digits = n_digits), nsmall = n_small))
  
}

ni <- ni %>% 
  mutate(
    mean_difference_lab = to_string(mean_difference),
    lo95_lab = to_string(lo95),
    hi95_lab = to_string(hi95),
    lab = paste(
      mean_difference_lab, 
      " (", 
      lo95_lab, 
      "-", 
      hi95_lab, 
      ")",
      sep = "")
  )

This parses correctly in console.

print(ni$lab)

And yet, the trailing zeros are removed from the string when I plot it as:

ni %>% 
  ggplot(aes(x = mean_difference, y = method)) +
  geom_point(
    size = 6,
    shape = 18) +
  geom_errorbarh(aes(
    xmin = lo95,
    xmax = hi95,
    height = 0
  )) +
  geom_text(aes(
    family = 'Courier',
    label = lab),
    parse = TRUE,
    nudge_y = -0.2) +
  scale_x_continuous(breaks = seq(- 6, 14, 2))

enter image description here

Can any help spare my blushes, please?

Lstat
  • 1,450
  • 1
  • 12
  • 18
dsthom
  • 3
  • 3
  • `to_string` is rounding to 1 decimal place. Are there any zeros left to plot after it? – Rui Barradas Jul 11 '20 at 16:32
  • Perhaps my explanation was not the best. Take hi95 for NC (6.977835) as an example. I want it plotted as 7.0, but it plots with 7 despite being stored as 7.0 in ni$lab. – dsthom Jul 11 '20 at 16:37
  • Setting `parse=FALSE` will keep the trailing 0. See https://stackoverflow.com/questions/43104021/stop-parsing-out-zeros-after-decimals-in-ggplot2s-annotate if you need `parse=TRUE` for some other reason. – r_alanb Jul 11 '20 at 16:46
  • 3
    Does this answer your question? [Stop parsing out zeros after decimals in ggplot2's annotate](https://stackoverflow.com/questions/43104021/stop-parsing-out-zeros-after-decimals-in-ggplot2s-annotate) – r_alanb Jul 11 '20 at 16:52

1 Answers1

3

if I understand your description you're getting: plot with parsed strings

but you want:

plot with non-parsed strings

The only thing I changed was the argument parse=TRUE to parse=FALSE, i. e.

ni %>% 
  ggplot(aes(x = mean_difference, y = method)) +
  geom_point(
    size = 6,
    shape = 18) +
  geom_errorbarh(aes(
    xmin = lo95,
    xmax = hi95,
    height = 0
  )) +
  geom_text(aes(
    family = 'Courier',
    label = lab),
    parse = FALSE, # changed
    nudge_y = -0.2) +
  scale_x_continuous(breaks = seq(- 6, 14, 2))

(note that there are some awkward spaces when using parse=FALSE - these however are already in the data, i. e. what's shown in the plot is the same as what you get when looking at ni$lab)

Does this answer your question?

datalowe
  • 589
  • 2
  • 7