plotly 在堆积条形图中悬停时给出不正确的文本
•浏览 1
plotly giving incorrect text on hover in stacked bar charts
我正在将 plotly 与 RStudio 一起使用。
plotly 在悬停时为图表中的堆叠条提供了不正确的文本,除了最顶部的条。
可重现的例子
df <- data.frame(
make = rep(c('Toyota', 'Honda', 'Volkswagen'),4),
segment =c(rep('high',3), rep('mid',3), rep('low',3), rep('entry',3) ),
sales=c( 25,35,75, 35,35,30, 45,25,10, 80, 80, 20)
)
df
make segment sales
1 Toyota high 25
2 Honda high 35
3 Volkswagen high 75
4 Toyota mid 35
5 Honda mid 35
6 Volkswagen mid 30
7 Toyota low 45
8 Honda low 25
9 Volkswagen low 10
10 Toyota entry 80
11 Honda entry 80
12 Volkswagen entry 20
p <- ggplot(df, aes(x=make, y=sales) ) +
geom_bar(stat="identity", aes(fill=segment), position ="stack")
ggplotly(p)> packageVersion('ggplot2')
[1] a€?2.2.1a€?
> packageVersion('plotly')
[1] a€?4.5.6a€?
>p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value", sales))) + geom_bar(stat="identity")
ggplotly(p)ggplotly(p, tooltip = c("text"))library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value:", sales))) + geom_bar(stat="identity")
ggplotly(p, tooltip = c("text"))
})
}
shinyApp(ui, server)
我在 RStudio 中得到的内容如下图所示。正如您所看到的,除了顶部堆叠的条之外,其他所有内容都在悬停
时给出了错误的文本
悬停时的文本不正确 - 基于 df 中的数据,sales 的正确值为 20 - 而是显示 135 - 数据 - 12 Volkswagen entry 20
悬停时的正确文本 - 仅观察到堆栈中最顶部的条。
df <- data.frame(
make = rep(c('Toyota', 'Honda', 'Volkswagen'),4),
segment =c(rep('high',3), rep('mid',3), rep('low',3), rep('entry',3) ),
sales=c( 25,35,75, 35,35,30, 45,25,10, 80, 80, 20)
)
df
make segment sales
1 Toyota high 25
2 Honda high 35
3 Volkswagen high 75
4 Toyota mid 35
5 Honda mid 35
6 Volkswagen mid 30
7 Toyota low 45
8 Honda low 25
9 Volkswagen low 10
10 Toyota entry 80
11 Honda entry 80
12 Volkswagen entry 20
p <- ggplot(df, aes(x=make, y=sales) ) +
geom_bar(stat="identity", aes(fill=segment), position ="stack")
ggplotly(p)> packageVersion('ggplot2')
[1] a€?2.2.1a€?
> packageVersion('plotly')
[1] a€?4.5.6a€?
>p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value", sales))) + geom_bar(stat="identity")
ggplotly(p)ggplotly(p, tooltip = c("text"))library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value:", sales))) + geom_bar(stat="identity")
ggplotly(p, tooltip = c("text"))
})
}
shinyApp(ui, server)
也许你可以尝试添加它:
df <- data.frame(
make = rep(c('Toyota', 'Honda', 'Volkswagen'),4),
segment =c(rep('high',3), rep('mid',3), rep('low',3), rep('entry',3) ),
sales=c( 25,35,75, 35,35,30, 45,25,10, 80, 80, 20)
)
df
make segment sales
1 Toyota high 25
2 Honda high 35
3 Volkswagen high 75
4 Toyota mid 35
5 Honda mid 35
6 Volkswagen mid 30
7 Toyota low 45
8 Honda low 25
9 Volkswagen low 10
10 Toyota entry 80
11 Honda entry 80
12 Volkswagen entry 20
p <- ggplot(df, aes(x=make, y=sales) ) +
geom_bar(stat="identity", aes(fill=segment), position ="stack")
ggplotly(p)> packageVersion('ggplot2')
[1] a€?2.2.1a€?
> packageVersion('plotly')
[1] a€?4.5.6a€?
>p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value", sales))) + geom_bar(stat="identity")
ggplotly(p)ggplotly(p, tooltip = c("text"))library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value:", sales))) + geom_bar(stat="identity")
ggplotly(p, tooltip = c("text"))
})
}
shinyApp(ui, server)
如果你只想显示销售额,你也可以使用:
df <- data.frame(
make = rep(c('Toyota', 'Honda', 'Volkswagen'),4),
segment =c(rep('high',3), rep('mid',3), rep('low',3), rep('entry',3) ),
sales=c( 25,35,75, 35,35,30, 45,25,10, 80, 80, 20)
)
df
make segment sales
1 Toyota high 25
2 Honda high 35
3 Volkswagen high 75
4 Toyota mid 35
5 Honda mid 35
6 Volkswagen mid 30
7 Toyota low 45
8 Honda low 25
9 Volkswagen low 10
10 Toyota entry 80
11 Honda entry 80
12 Volkswagen entry 20
p <- ggplot(df, aes(x=make, y=sales) ) +
geom_bar(stat="identity", aes(fill=segment), position ="stack")
ggplotly(p)> packageVersion('ggplot2')
[1] a€?2.2.1a€?
> packageVersion('plotly')
[1] a€?4.5.6a€?
>p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value", sales))) + geom_bar(stat="identity")
ggplotly(p)ggplotly(p, tooltip = c("text"))library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value:", sales))) + geom_bar(stat="identity")
ggplotly(p, tooltip = c("text"))
})
}
shinyApp(ui, server)
使用闪亮:
df <- data.frame(
make = rep(c('Toyota', 'Honda', 'Volkswagen'),4),
segment =c(rep('high',3), rep('mid',3), rep('low',3), rep('entry',3) ),
sales=c( 25,35,75, 35,35,30, 45,25,10, 80, 80, 20)
)
df
make segment sales
1 Toyota high 25
2 Honda high 35
3 Volkswagen high 75
4 Toyota mid 35
5 Honda mid 35
6 Volkswagen mid 30
7 Toyota low 45
8 Honda low 25
9 Volkswagen low 10
10 Toyota entry 80
11 Honda entry 80
12 Volkswagen entry 20
p <- ggplot(df, aes(x=make, y=sales) ) +
geom_bar(stat="identity", aes(fill=segment), position ="stack")
ggplotly(p)> packageVersion('ggplot2')
[1] a€?2.2.1a€?
> packageVersion('plotly')
[1] a€?4.5.6a€?
>p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value", sales))) + geom_bar(stat="identity")
ggplotly(p)ggplotly(p, tooltip = c("text"))library(shiny)
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output) {
output$plot <- renderPlotly({
p <- ggplot(df, aes(x=make, y=sales, fill=segment, text=paste("sales original value:", sales))) + geom_bar(stat="identity")
ggplotly(p, tooltip = c("text"))
})
}
shinyApp(ui, server)