Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

In the tooltip of the bar graph, it always displays text like this paste(hp, 'hp') 335 hp. What I want is just like this format: hp: 335 hp.

library(tidyverse)

library(plotly)

mtcars$cars = row.names(mtcars)

g = mtcars %>%

        arrange(desc(hp)) %>% 

        head(., 10) %>% 

        ggplot(aes(x= reorder(cars, hp), y=hp,

                   text=cars, text1 = paste(hp, 'hp') ))+

        geom_bar(stat='identity', fill='darkred')+

        coord_flip()

ggplotly(g,  tooltip = c("text","text1") )

1 Answer

0 votes
by (107k points)

You can try to put all the text into one line and then use a new line break \n:

library(tidyverse)

library(plotly)

mtcars$cars = row.names(mtcars)

g = mtcars %>%

  arrange(desc(hp)) %>% 

  head(., 10) %>% 

  ggplot(aes(x= reorder(cars, hp), y=hp,

             text = paste0(cars, "\n", "hp: ", hp) ))+

  geom_bar(stat='identity', fill='darkred')+

  coord_flip()

ggplotly(g,  tooltip = c("text","text1") )

If you want to know more about R then do check out the R programming course

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...