ggvis
ggvis copied to clipboard
Categorical axis misalignment with reactive data w/ layers_rects
When a ggvis chart is created using a reactive dataset with a variable number of categories for a categorical axis, rectangles drawn using width=band() as well as height=band() will not line up with the axis ticks properly.
Code for repro is below. To reproduce, run the code, slide the slider to 5 observations and note that rectangles are not aligned with x-axis ticks as you would expect.
library(dplyr)
library(ggvis)
library(shiny)
server <- function(input, output) {
rData <- reactive(mtcars %>%
mutate(name = rownames(mtcars)) %>%
head(input$obs))
rData %>%
ggvis(~name,~mpg,y2=~0,width=band()) %>%
layer_rects() %>%
bind_shiny("plot")
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 1, max = 32, value = 100)
),
mainPanel(ggvisOutput("plot"))
)
)
shinyApp(ui = ui, server = server)
Noticed something else. If you manually drag-resize the ggvis plot after this issue occurs, the plot re-adjusts the axis and alignment is restored.