【问题标题】:Show lower and upper bounds in geom_bin histogram (ggplot2)在 geom_bin 直方图中显示下限和上限(ggplot2)
【发布时间】:2018-07-20 10:19:15
【问题描述】:

在下面的直方图中,如何在 ggplotly 中包含 每个 bin 的下限和上限 作为工具提示文本?

library(ggplot2)
library(plotly)
csub<-data.frame(Anomaly10y = rpois(50,5))
p <-  ggplot(csub,aes(x=Anomaly10y)) + 
  stat_bin(binwidth=1, aes(label1 = min(Anomaly10y, na.rm = T), label2 = 
  max(Anomaly10y, na.rm = T))) 
ggplotly(p, tooltip = c("label1", "label2"))

提前致谢

【问题讨论】:

    标签: r ggplot2 histogram plotly ggplotly


    【解决方案1】:

    也许有更好的解决方案,但你可以欺骗它,使用geom_bar() 来得到你想要的。这是一个替代方案:

    #first extract the data from stat_bin
    p <-  ggplot(csub,aes(x=Anomaly10y)) + stat_bin(binwidth=1) 
    
    temp <- layer_data(p, 1)
    #ggplot
    pp <- ggplot(data = temp, aes(x =x,y=y, label1 = xmin, label2 = xmax)) + 
    geom_bar(stat = 'identity', fill = "darkgrey", width = 1)
    

    然后用ggplotly进行绘图:

    ggplotly(pp)
    

    ggplotly(pp, tooltip = c("label1", "label2"))
    

    也许有人可以解决这个选项,但是:

    pp <-  ggplot(csub,aes(x=Anomaly10y)) + 
        stat_bin(binwidth=1)  + 
    stat_bin(binwidth=1, geom="text", aes(label=..xmin..), vjust = 2)  + 
    stat_bin(binwidth=1, geom="text", aes(label=..xmax..)) 
    ggplotly(pp)
    

    我只得到一个标签,而不是两个。

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      相关资源
      最近更新 更多