【问题标题】:ggplotly with geom_bar shows wrong y-axis values when moving the cursor onto the bar将光标移动到条上时,带有 geom_bar 的 ggplotly 显示错误的 y 轴值
【发布时间】:2019-09-20 03:06:24
【问题描述】:

我有以下代码创建条形图:

require(data.table)
require(plotly)
require(ggplot2)

df1 <- data.table(Time = seq(50, 290, 30), Enter = c(155000, 400000, 950000, 950000, 1000000, 1100000, 1100000, 1100000, 1150000), 
Exit = c(150000, 165000, 167000, 225000, 500000, 560000, 562000, 564000, 590000))

df1 <- melt(df1[,c('Time','Enter','Exit')],id.vars = 1)
df1$Time <- as.factor(df1$Time)
df1$value <- format(df1$value, format="f", big.mark=",", scientific = F, digits = 0)
# Create Barplot of In and outflows
ggplotly(tooltip = c("y", "x", "colour"), p =
           ggplot(df1, aes(x = Time, y = value)) + 
           geom_bar(aes(fill = variable), stat = 'identity', position = 'dodge', colour = 'black') + 
           scale_fill_manual(name = "", labels = c('Enter', 'Exit'), values= c('chartreuse4', 'brown')) +
           labs(x = 'Time', y ='Value')) 

当我将鼠标光标移动到条形以查看条形具有哪个值时,它没有显示(即第一个绿色条形)155,000 但 2。这是为什么以及如何修复它以使其显示正确的数字?

【问题讨论】:

    标签: r ggplot2 geom-bar ggplotly


    【解决方案1】:

    必须转换成我认为的因子!那么它应该可以正常工作。

    图书馆和数据:

    require(data.table)
    require(plotly)
    require(ggplot2)
    
    df1 <- data.frame(Time = seq(50, 290, 30), Enter = c(155000, 400000, 950000, 950000, 1000000, 1100000, 1100000, 1100000, 1150000), 
                      Exit = c(150000, 165000, 167000, 225000, 500000, 560000, 562000, 564000, 590000))
    

    解决方案 1:以因子格式使用 value

       df1 <- melt(df1[,c('Time','Enter','Exit')],id.vars = 1)
        df1$Time <- factor(df1$Time)
        #df1$value <- format(df1$value, format="f", big.mark=",", scientific = F, digits = 0)
    
        # Create Barplot of In and outflows
        ggplotly(tooltip = c("y", "x", "colour"), p =
                   ggplot(df1, aes(x = Time, y = value)) + 
                   geom_bar(aes(fill = variable), stat = 'identity', position = 'dodge', colour = 'black') + 
                   scale_fill_manual(name = "", labels = c('Enter', 'Exit'), values= c('chartreuse4', 'brown')) +
                   labs(x = 'Time', y ='Value')) 
    

    解决方案 2:将 value 保留为数字格式,但仍保留正确的 tooltip 以逗号分隔,请使用以下版本:

    df1 <- melt(df1[,c('Time','Enter','Exit')],id.vars = 1)
    df1$Time <- factor(df1$Time)
    df1$value <- format(df1$value, format="f", big.mark=",", scientific = F, digits = 0)
    
    # Create Barplot of In and outflows
    p=ggplot(df1, aes(x = Time, y = value, text = paste0("Value:", value)) ) + 
               geom_bar(aes(fill = variable), stat = 'identity', position = 'dodge', colour = 'black') + 
               scale_fill_manual(name = "", labels = c('Enter', 'Exit'), values= c('chartreuse4', 'brown')) +
               labs(x = 'Time', y ='Value')
    
    ggplotly(p, tooltip = c("x","text"))
    

    【讨论】:

    • 这行得通。但是,我仍然想为图中的数字引入 1k 逗号分隔符。问题似乎是value 列在使用format() 时被转换为字符。如果我不使用format(),我会在图中得到正确的数字,但缺少逗号分隔符。
    猜你喜欢
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 2018-11-04
    • 2019-05-27
    • 2021-07-28
    • 2022-12-01
    相关资源
    最近更新 更多