I am using plotly 4.8 with ggplot2 3.0.0, and trying to add and align text labels to my scatter plots. However, it seems the hjust
parameter is being ignored by plotly in geom_text(aes(....), hjust = "left")
. (Also tried hjust = 0
.)
GGPLOT OUTPUT
See it renders fine in plot window as a ggplot with labels left aligned.
PLOTLY OUTPUT
But the alignment is lost in conversion, and the text is centered.
So the question, is fixing this alignment possible with plotly?
TEST EXAMPLE CODE
library(ggplot2)
library(data.table)
library(plotly)
data(mtcars)
plotdata <- as.data.table(mtcars)
plotdata$carname <- rownames(mtcars)
# take a small demo subset
plotdata <- plotdata[1:10,]
gg <- ggplot(plotdata, aes(x = wt, y = mpg, label = carname)) +
geom_point() + theme_minimal()
gg <- gg + geom_text(aes(label = carname),
size = 2,
hjust = "left")
print(gg)
# convert ggplot
p <- ggplotly(gg)
p
You just need to add text position textposition = "right"
:
ggplotly(p) %>% style(textposition = "right")
Output:
Ref: https://github.com/ropensci/plotly/issues/769
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With