【问题标题】:Missing tooltips and redundant legend with ggplotly using geom_bar使用 geom_bar 的 ggplotly 缺少工具提示和冗余图例
【发布时间】:2017-05-17 15:22:15
【问题描述】:

我正在尝试重现我找到的 here 的 ggplot geom_bar 示例。 代码比较简单

library(ggplot2)
library(plotly)

dat <- data.frame(
time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23))

# No legend, since the information is redundant
ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
geom_bar(colour="black", stat="identity") +
guides(fill=FALSE)

ggplotly()

在 ggplot 中,填充图例按预期隐藏,但情节与示例不同:

  • 包含图例
  • 工具提示不起作用

因为我认为这是与软件包版本相关的问题,所以我附上了我的 sessionInfo()

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=German_Austria.1252  LC_CTYPE=German_Austria.1252    LC_MONETARY=German_Austria.1252 LC_NUMERIC=C                    LC_TIME=German_Austria.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] plotly_4.5.6   ggplot2_2.2.1  nvimcom_0.9-19

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8       tidyr_0.6.0       viridisLite_0.1.3 digest_0.6.10     dplyr_0.5.0       assertthat_0.1    grid_3.3.2        plyr_1.8.4        R6_2.2.0          jsonlite_1.1      gtable_0.2.0      DBI_0.5-1        
[13] magrittr_1.5      scales_0.4.1      httr_1.2.1        lazyeval_0.2.0    tools_3.3.2       htmlwidgets_0.8   purrr_0.2.2       munsell_0.4.3     base64enc_0.1-3   colorspace_1.3-1  htmltools_0.3.5   tibble_1.2 

【问题讨论】:

  • p &lt;- ggplotly();p$x$data[[1]]$text &lt;- c(p2$x$data[[1]]$text, "") ;p$x$data[[2]]$text &lt;- c(p2$x$data[[2]]$text, "") ;p?见stackoverflow.com/questions/41184959/…
  • 谢谢!这(使用 p 而不是 p2)解决了工具提示的问题。对传奇有什么想法吗?
  • GitHub 上提交了错误报告。

标签: r ggplot2 plotly


【解决方案1】:

GitHub 发布的解决方案bcdunbar 发布的说

theme(legend.position = "none") +

转换为ggplotly 中的图例抑制。

【讨论】:

    【解决方案2】:

    我刚刚注意到lukeA 引用的solution 还提供了一种通过在 ggplotly 对象中手动禁用它来消除图例的方法。比如这样:

    library(ggplot2)
    library(plotly)
    
    dat <- data.frame( time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), total_bill = c(14.89, 17.23))
    
    # No legend, since the information is redundant
    p <- ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + geom_bar(colour="black", stat="identity") + guides(fill=FALSE)
    p <- ggplotly(p)
    
    
    for (i in 1:nrow(dat)){
        p$x$data[[i]]$text <- c(p$x$data[[i]]$text, "") 
        p$x$data[[i]]$showlegend <- FALSE
    }
    p
    

    仍然是一个肮脏的解决方案,但它可以完成工作......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2018-12-21
      • 2020-05-13
      • 1970-01-01
      相关资源
      最近更新 更多