2017-02-20 10 views
2

私はプロット的なインタラクティブな図形でラベルとしてテキストを持ち、詳細についてはこれらのラベルの上にカーソルを置いておきたいと思います。テキストラベルとホバーテキストをプロットして使用できますか?

しかし、プロットしても、同じコード行にテキストラベルとホバーテキストが表示されません。私は必然的にテキストラベルの後ろにあり、単純な散点を使用したくない。

テキストラベルを表示してテキストを表示できるように、以下のコードを修正する方法はありますか?

ありがとうございました。あなたが解決策に似た何かができる

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

答えて

4

はつまり、単にhovertextための散布図を作成し、注釈などのテキストを追加し、hereを示唆しました。

enter image description here

以下のスニペットを参照してください。

# 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 
関連する問題