2016-06-28 11 views
5

Rのggplotに基づいてPlotlyプロットを作成しています。ホバーボックスのテキストのサイズを大きくしたいと思います。Plotlyのホバーテキストのサイズを変更

library(plotly) 
library(ggplot2) 

d <- data.frame(a = sample(1:50, 30, T), 
       b = sample(1:50, 30, T), 
       col = factor(sample(1:3, 30, T))) 

gg <- ggplot() + geom_point(aes(x = a, y = b, color = col), data = d) 

p <– plotly_build(gg) 
p 

ホバーテキストのサイズを変更する方法はありますか?

+0

Rでこれを達成する正しい方法は私が信じていることについて私の答えを見てください – rmg

答えて

4

現在、プロットを介してホバーの外観を直接定義するための追加の属性を渡す組み込みの方法はありません(github issue #102参照)。ただし、問題の説明には、ホバーテキストに使用されるクラスの名前が表示されます(.hovertext)。最も簡単な解決策は、あなたをHTMLファイルとしてプロットして保存し、HTMLの<head>のどこかに手作業でCSSを追加することです。凡例のテキストのサイズを変更したい場合は、.legendtextの行を消去しない場合はそのままにしてください。

<style type="text/css"> 
.hovertext text { 
    font-size: 100px !important; 
} 
.legendtext { 
    font-size: 30px !important; 
} 
</style> 

手で行う代わりにRを使用してCSSを注入する場合は、いくつかの選択肢があります。

# the CSS we want to inject 
css <- ' 
<style type="text/css"> 
.hovertext text { 
    font-size: 100px !important; 
} 
.legendtext { 
    font-size: 30px !important; 
} 
</style>' 

library(plotly) 
library(htmltools) 
library(htmlwidgets) 

1:作成

x <- as.widget(p)         # convert to htmlwidget object 
saveWidget(x, file="test_edited_1.html")   # and save to file 
l <- readLines("test_edited_1.html")    # read file 
h <- paste(l, collapse= " ")    
hh <- strsplit(h, "<head>")[[1]]     # split where head appears 
h.new <- paste(hh[1], css, hh[-1], collapse=" ") # insert CSS 
writeLines(h.new, "test_edited_1.html")   # write back to file 

2の後にHTMLファイルを変更:第二作品が、私はわからないものの、HTMLファイルが

x <- as.widget(p)         # convert to htmlwidget object 
# add a the code directly into <head> using `htmltools::htmlDependency` 
x$dependencies <- list(
    htmlDependency(
     name = "custom", 
     version="1", 
     src="", 
     head=css) 
    ) 
saveWidget(x, file="test_edited_2.html") 

作成されたオブジェクトを変更しますhtmlDependencyが適切に使用されている場合。

結果は

enter image description here

1

あなたがホバーテキストの様々な特性を変更することができるようにplotly最近updateがありました。

plot_ly(x=c(1:42), 
     y=c(1:42), 
     text=c(1:42), 
     type="bar")%>% 
    layout(
    title = paste("Top 42"), 
    hoverlabel = list(font=list(size=10)) 
) 

フォントの色、bgcolorなどを変更するオプションもあります。

関連する問題