首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >文字标签和悬停文字能被巧妙地使用吗?

问文字标签和悬停文字能被巧妙地使用吗?
EN

Stack Overflow用户
提问于 2017-02-20 06:09:07
回答 1查看 4.9K关注 0票数 4

我希望有文字作为标签,以一个巧妙的互动数字和悬停在这些标签上,以获得更多的信息。

但是,不允许我在同一代码行中使用文本标签和悬停文本。我拼命追求文字标签,不想使用一个简单的散点点。

是否有一种方法可以修复下面的代码,这样就可以显示文本标签并悬停文本?

谢谢。

代码语言:javascript
复制
# 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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-20 13:47:08

您可以执行类似于建议的here解决方案的操作,即仅为悬停文本创建一个散点图,并将文本添加为注释。

​

​

看下面的片段。

代码语言:javascript
复制
# Load packages
require(plotly)
# Example data
data(iris)

# Create new columns - with more information
iris$Symbol <- c("Se", "Ve", "Vi")[iris$Species]
iris$PlantedBy <- c("Bruce", "Joe", "Eliza")[iris$Species]

# Create scatter plot with no markers but hovertext
p <- plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width, 
            type = 'scatter',
        mode = 'markers',
        hoverinfo = 'text',
        text = ~paste('Species: ', Species, 
                      '</br> Planted by: ', PlantedBy),
        marker = list(size=1)) %>%
#add annotations for text symbols
add_annotations(
    x= iris$Sepal.Length,
    y = iris$Sepal.Width,
    text = iris$Symbol,
    showarrow = F,
    xref = "x",
    yref = "y"
    )
p
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42337560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档