作者open272 (好ㄘ的肉)
看板R_Language
标题[问题] plotly套件画图-调整标记
时间Mon Dec 12 21:39:35 2016
[问题类型]:
程式谘询
[软体熟悉度]:
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
利用plotly套件画bar plot时,要调整bar标注时发现找不到准确的X轴位置对应到相对
应的bar中心,参考下图
http://imgur.com/y8sUzSM
[程式范例]:
library(plotly)
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)
y <- c(20,12,14,18,23,29)
p <- plot_ly(data, x = Animals, y = SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
add_trace(y = LA_Zoo, name = 'LA Zoo') %>%
layout(yaxis = list(title = 'Count'), barmode = 'group',
annotations = list( x =data$Animals,y = y, text = y,
xanchor = 'center', yanchor = 'bottom',
showarrow = FALSE))
[环境叙述]:
R version 3.2.5 Patched (2016-05-05 r70791)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.12.1 (unknown)
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] RJDBC_0.2-5 rJava_0.9-8 DBI_0.4-1 corrplot_0.77
[6] RColorBrewer_1.1-2 googlesheets_0.2.0 gridExtra_2.2.1 plotly_4.5.2
[11] sp_1.2-3 ggplot2_2.1.0 DT_0.2 stringr_1.0.0
[16] tidyr_0.4.1 dplyr_0.4.3
loaded via a namespace (and not attached):
[1] Rcpp_0.12.6 cellranger_1.0.0 plyr_1.8.4 base64enc_0.1-3 t
oo
[6] digest_0.6.10 evaluate_0.9 jsonlite_1.1 tibble_1.2 g
ta
[11] lattice_0.20-33 viridisLite_0.1.3 yaml_2.1.13 parallel_3.2.5 k
ni
[16] httr_1.1.0 htmlwidgets_0.7 gtools_3.5.0 grid_3.2.5 R
6_
[21] rmarkdown_1.0 foreign_0.8-66 purrr_0.2.1 magrittr_1.5 s
ca
[26] htmltools_0.3.5 assertthat_0.1 colorspace_1.2-6 stringi_1.1.1 l
az
[31] munsell_0.4.3
[关键字]:
plotly , 视觉化
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.119.120.6
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1481549977.A.692.html
※ 编辑: open272 (140.119.120.6), 12/12/2016 21:43:48
1F:→ clansoda: 痾,我有点看不懂叙述,我看你的X轴的值蛮正的阿 12/12 21:46
2F:→ clansoda: 你是要说你的那几个数字吗? 12/12 21:47
3F:→ clansoda: 我不清楚plotly的写法所以我提供一下ggplot的写法 12/12 21:48
4F:→ clansoda: + geom_text(label = 你要放上去的值, h_just = 用来调 12/12 21:49
5F:→ clansoda: 整的argument,你写完ggplot以後 最後用ggplotly可以得 12/12 21:50
6F:→ clansoda: 到跟plotly一样的结果,仅供参考,plotly的需要其他人 12/12 21:50
7F:→ clansoda: 来提供 12/12 21:50