1
votes

When I add my own text into the tooltips for ggplotly the order of the text changes. In my example the order is Portuegese then German in my text, however on the plot the order is different.

How do I fix this?

Thanks

library(ggplot2);
library(plotly);

language <- c("de","es","hi","pt","sv","en")
averageRating <- c(6,4,3,9,10,30)
my_data <- data.frame(language, averageRating)
text <- c("Portuegese","German","Spanish","Hindi","Swedish","English")
p <- ggplot(data=my_data, aes(x=language, y=averageRating, text = text)) +
geom_bar(stat = "identity")

ggplotly(p, tooltip = c("text"))
2

2 Answers

0
votes

Maybe you should try factor within aes when applying ggplot, e.g.,

ggplot(data = my_data, aes(x = factor(language, levels = language), y = averageRating, text = text))
0
votes

To fix this I added a new column in my dataframe with the name I wanted displayed in the tooltip. I then set the "text" inside the aesthetic to that column name.

library(ggplot2);
library(plotly);
my_data <- read.csv("languageVsRating.csv")
p <- ggplot(data=my_data, aes(x=reorder(language,-averageRating), y=averageRating, text = fullName)) +
geom_bar(stat = "identity") +
labs(x="Film Language",y="Average Rating")+
geom_hline(aes(yintercept = 6.9,linetype="Mean"),colour='red',size=1)+
geom_hline(aes(yintercept = 6.7,linetype="Median"),colour='blue',size=1)+
scale_linetype_manual(name = "",values = c(2,2),guide = guide_legend(override.aes = list(color = c("red", "blue"))))

ggplotly(p, tooltip = c("text","y"))

There is another column inside languageVsRating.csv with the language full name