[問題] plotly套件畫圖-調整標記

看板R_Language作者 (好ㄘ的肉)時間9年前 (2016/12/12 21:39), 9年前編輯推噓0(007)
留言7則, 1人參與, 最新討論串1/1
[問題類型]: 程式諮詢 [軟體熟悉度]: 入門(寫過其他程式,只是對語法不熟悉) [問題敘述]: 利用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://www.ptt.cc/bbs/R_Language/M.1481549977.A.692.html ※ 編輯: open272 (140.119.120.6), 12/12/2016 21:43:48

12/12 21:46, , 1F
痾,我有點看不懂敘述,我看你的X軸的值蠻正的阿
12/12 21:46, 1F

12/12 21:47, , 2F
你是要說你的那幾個數字嗎?
12/12 21:47, 2F

12/12 21:48, , 3F
我不清楚plotly的寫法所以我提供一下ggplot的寫法
12/12 21:48, 3F

12/12 21:49, , 4F
+ geom_text(label = 你要放上去的值, h_just = 用來調
12/12 21:49, 4F

12/12 21:50, , 5F
整的argument,你寫完ggplot以後 最後用ggplotly可以得
12/12 21:50, 5F

12/12 21:50, , 6F
到跟plotly一樣的結果,僅供參考,plotly的需要其他人
12/12 21:50, 6F

12/12 21:50, , 7F
來提供
12/12 21:50, 7F
文章代碼(AID): #1OJgYPQI (R_Language)
文章代碼(AID): #1OJgYPQI (R_Language)