简体   繁体   中英

Why does the order of my tooltips change when using ggplotly?

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"))

Maybe you should try factor within aes when applying ggplot , eg,

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM