0

I'm making an interactive plot with ggplotly, where I have text on each bar. However, when I use ggplotly some of the text disappears, even though it is present in ggplot. Any help?

p8<-ggplot(full4, aes(x=rank, group = Employer, 
                                     fill = as.factor(Employer), frame = Year)) +
 geom_tile(aes(y=Employees/2,height = Employees,
                                 width = 0.9, text=Employer,text2=Employees), alpha = 0.8, color = NA) +
 geom_text(aes(y=0, label = Employer), color="black", hjust=0) +
 coord_flip(clip = "off", expand = FALSE) +
 scale_y_continuous(labels = scales::comma) +
 scale_x_reverse() +
 guides(color = FALSE, fill = FALSE) +
 labs(title='Top 10 Employers of Rice Students', x = "", y = "Number of Graduates") +
 theme_bw()+
 theme(plot.title = element_text(hjust = 0, size = 22),
                 axis.ticks.y = element_blank(),  # These relate to the axes post-flip
                 axis.text.y  = element_blank(),  # These relate to the axes post-flip
                 plot.margin = margin(1,1,1,4, "cm"),
                 legend.position = "none")

ggplotly(p8, tooltip = c("text","text2")) %>% 
  style(textposition = "right")

Ggplot object: (very overlapping due to the animation, sorry) enter image description here

Ggplotly: enter image description here Bar text missing

  • Does it work for ggplot bars (without running ggplotly)? – bird May 11 '21 at 16:34
  • Just tried it, and it appears to work. I guess there's a problem with ggplotly. However, I realized that what I was doing was aligning it to the right of the bar and not starting at the beginning of the bar. When I change it from y=Employees to y=0 in the aesthetic mapping, it works in ggplot, but not ggplotly. – Michael Kelley May 11 '21 at 16:38
  • I think nothing is wrong with `ggplotly`. It works for `ggplot` because `hjust` is implemented for `ggplot` and there is probably another way to modify the alignment for `ggplotly`. If you share some reproducible data, I am sure your problem would be solved. – bird May 11 '21 at 16:43
  • Ok, I somewhat got it to work, but I have a new problem now. I will post a new question and resolve this one. – Michael Kelley May 11 '21 at 16:46
  • can you please explain how you solved it? (I am learning from these posts :)) – bird May 11 '21 at 16:47
  • I realized that ggplotly was the issue and this post fixed it: https://stackoverflow.com/questions/52077666/r-plotly-ignoring-text-label-alignment-hjust – Michael Kelley May 11 '21 at 16:52

0 Answers0