[英]Why does the order of my tooltips change when using ggplotly?
当我将自己的文本添加到 ggplotly 的工具提示中时,文本的顺序会发生变化。 在我的示例中,我的文本中的顺序是葡萄牙语,然后是德语,但是在 plot 上,顺序是不同的。
我该如何解决?
谢谢
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"))
也许你应该在应用ggplot
时尝试使用aes
中的factor
,例如,
ggplot(data = my_data, aes(x = factor(language, levels = language), y = averageRating, text = text))
为了解决这个问题,我在 dataframe 中添加了一个新列,其中包含我希望在工具提示中显示的名称。 然后,我将美学内的“文本”设置为该列名。
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"))
languageVsRating.csv 里面还有另一列,语言全名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.