【问题标题】:How to avoid overplotting with text_layer?如何避免使用 text_layer 过度绘制?
【发布时间】:2016-09-29 15:34:17
【问题描述】:

我有一张图表,其中layer_text 元素重叠:

data.frame(label=c("First", "Second","First","Second"), x=c(100,100,20,20),y=c(100,100,20,20)) %>% 
  ggvis(~x,~y) %>% 
  layer_text(text:=~label)

我想偏移文本以使其可读。我正在考虑在== 时调整y 的值,但图表需要放入闪亮的应用程序中,我最终可能会有超过 2 个重叠文本。这就是为什么我想知道是否有任何包/属性可以提供更稳定的解决方案。

解决方案应如下所示:

data.frame(label=c("First", "Second","First","Second"),    x=c(100,100,20,20),y=c(100,98,20,18)) %>% 
  ggvis(~x,~y) %>% 
  layer_text(text:=~label)

【问题讨论】:

    标签: r ggvis


    【解决方案1】:

    标签放置是一个众所周知的棘手问题。 ggrepel 实现了一个不错的算法解决方案,但它不适用于 ggvis,只有 ggplot2

    library(ggplot2)
    library(ggrepel)
    library(magrittr)
    
    data.frame(label=c("First", "Second","First","Second"), 
               x=c(100,100,20,20),
               y=c(100,100,20,20)) %>% 
      ggplot(aes(x,y,label=label)) +
      geom_text_repel() +
      theme_bw()
    

    【讨论】:

      猜你喜欢
      • 2011-04-04
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2011-08-16
      • 2016-03-16
      相关资源
      最近更新 更多