简体   繁体   中英

ggplotly with geom_bar shows wrong y-axis values when moving the cursor onto the bar

I have the following code creating a bar plot:

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')) 

When I move the mouse cursor to the bars to see which value the bar has it does not show (ie for the first green bar) 155,000 but 2. Why's that and how can I fix it such that it shows the correct number?

The value must be converted to a factor I think. Then it should work fine.

Libraries and the data:

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))

Solution 1: Use value in a factor format:

   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')) 

Solution 2: to keep value with a numeric format but still the right tooltip separated by comma use the below version:

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM