Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Add_trace to a ggplotly object

I tried to add_trace to a ggplotly object:
p <- ggplot(mtcars, aes(x = disp, y= am, color = as.factor(cyl)))+geom_point()
ggplotly§ %>% add_trace(data = mtcars,x = ~disp, y= ~am,type=‘scatter’,mode=‘marker’)
The code work well in this case.

However, when y-axis is a character, the value of the added trace is not right.
mtcars$am2 <- as.character(mtcars$am)
p <- ggplot(mtcars, aes(x = disp, y= am2, color = as.factor(cyl)))+geom_point()
ggplotly§ %>% add_trace(data = mtcars,x = ~disp, y= ~am2,type=‘scatter’,mode=‘marker’)

Is there a way to fix this?