[问题] ggplot转plotly 自订hovertext内容

楼主: parcequetoi (Sky)   2017-07-29 18:44:15
[问题类型]:
程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
[软件熟悉度]:
使用者(已经有用R 做过不少作品)
[问题叙述]:
下方的程式范例直接执行可出现一个交互式图。
当鼠标移到线上的时候,在“挂牌日期”那个项目已经从日期被自动转成数字,
但我想要的是原本日期的样子(例如2017/07/28)。
我查过资料,知道可以透过下列方法自订hovertext:
p <- plotly_build(g)
my.text <- paste0("交易种类:", usd$交易种类, "\n",
"挂牌日期:", strftime(usd$挂牌日期, "%Y/%m/%d"), "\n",
"汇率:", usd$汇率)
style(p, text = my.text, hoverinfo = "text", traces = 1:4)
但是这样画的颜色与类别对应结果是错的。
目前遇到的最大问题是:
我不知道我的character vector究竟要用什么顺序,
每一个elements才会顺利对到正确的图上位置。
常常hovertext是改了,但是hovertext文字显示的交易种类却跟线的颜色不符。
即使符合了,我也没有把握数列的顺序到底是不是正确的。
或是这种例子有直接用plotly作图的方法?Thanks in advance!!
已参考过下列文章:
1. R_Language (#1O6p-6pq)
2. https://plot.ly/r/time-series/
3. http://www.r-graph-gallery.com/2017/06/07/get-the-best-from-ggplotly/
[程式范例]:
https://gist.github.com/anonymous/c39f842543e49c5963671e333eb396ab
[环境叙述]:
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] shiny_1.0.3 shinydashboard_0.6.1 reshape2_1.4.2
[4] plotly_4.7.0 ggplot2_2.2.1 magrittr_1.5
[7] rvest_0.3.2 xml2_1.1.1 httr_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.11 compiler_3.4.0 plyr_1.8.4
[4] bindr_0.1 tools_3.4.0 digest_0.6.12
[7] evaluate_0.10.1 jsonlite_1.5 tibble_1.3.3
[10] gtable_0.2.0 viridisLite_0.2.0 pkgconfig_2.0.1
[13] rlang_0.1.1 crosstalk_1.0.0 yaml_2.1.14
[16] curl_2.8.1 bindrcpp_0.2 knitr_1.16
[19] dplyr_0.7.2 stringr_1.2.0 htmlwidgets_0.9
[22] rprojroot_1.2 grid_3.4.0 glue_1.1.1
[25] data.table_1.10.4 R6_2.2.2 rmarkdown_1.6
[28] purrr_0.2.2.2 tidyr_0.6.3 selectr_0.3-1
[31] backports_1.1.0 scales_0.4.1 htmltools_0.3.6
[34] assertthat_0.2.0 mime_0.5 colorspace_1.3-2
[37] xtable_1.8-2 httpuv_1.3.5 labeling_0.3
[40] stringi_1.1.5 lazyeval_0.2.0 munsell_0.4.3
[关键字]:
ggplot2, plotly, ggplotly, hovertext
作者: abc2090614 (casperxdd)   2017-07-29 20:09:00
看一下我之前回的 #1O6p-6pq 可能是你要的用hoverinfo, 然后text部份可以自订
楼主: parcequetoi (Sky)   2017-07-29 20:16:00
那篇我看过囉,谢谢你,我有写我的尝试结果在内文里主要是现在看到的例子hover中都没有附上原类别变项我找到出错的原因了,是因为traces = 1:4这个参数如此一来my.text[1]会被用在所有四条线的第一点my.text[2]则会出现在所有四条线的第二个资料点

Links booklink

Contact Us: admin [ a t ] ucptt.com