Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Ggplot2: vjust of geom_text doesn't work in bars

Hello,

ggplot has the option of displaying a text next to the displayed values.
In a bar plot it can be for example the displayed values:

  SampleData <- data.frame(Name = c("Peter", "Andrew"), Result = c(6, 9))
  SamplePlot <- ggplot(SampleData, aes(Name, Result)) + geom_bar(stat = "identity") + 
        geom_text(aes(label=Result), color = "white", vjust = 4)

By default that text is placed at the top border of the bar, although with vjust it can be set above / below.

When I use the plotly_build function that vertical alignement is lost:

  plotly_build(SamplePlot)

How should I make the ggplot or the plotly_build so that the geom_text keeps a good alignement?

Thanks!

So based on https://plot.ly/r/reference/#scatter-textposition I am not sure plotly_js directly supports this. If I replicate the chart in using plot_ly()

plot_ly(SampleData, x = ~Name, y = ~Result, text = ~Result) %>% add_bars() %>% add_text(textposition = "bottom right")

I get this which is the most I could do