【发布时间】:2017-02-20 06:09:07
【问题描述】:
我希望在一个有情节的交互式图形中将文本作为标签,并将鼠标悬停在这些标签上以获取更多信息。
但是,plotly 不允许我在同一行代码中使用文本标签和悬停文本。我非常关注文本标签,不想使用简单的散点。
有没有办法修复下面的代码,以便显示文本标签和悬停文本?
谢谢。
# Load packages
require(ggplot2)
require(plotly)
# Example data
data(iris)
str(iris)
# Create new columns - with more information
iris$Symbol <- c("Se", "Ve", "Vi")[iris$Species]
iris$PlantedBy <- c("Bruce", "Joe", "Eliza")[iris$Species]
# Create in ggplot
ggplot(iris, aes(x = Sepal.Length, y =Sepal.Width, colour = Species,
label = Symbol)) +
geom_text(fontface = "bold", size = 6) +
theme_classic() +
theme(legend.position = "none")
# Plotly - point with hover text
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = 'scatter',
mode = 'text',
text = ~Symbol)
# Plotly - point with hover text (does not work)
plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, type = 'scatter',
mode = 'text',
text = ~Symbol,
hoverinfo = 'text',
text = ~paste('Species: ', Species,
'</br> Planted by: ', PlantedBy))
【问题讨论】: