I need to have with ggvis in shiny the labels= axis feature.
In R the labels axis argument allow to change the x labels (adding a Kb format), that keep the order and the relative distance between items:
mtcars <- mtcars[1:10, ]
my_data <- mtcars[order(mtcars$disp),]
xpos <- sort(mtcars$disp)
plot(my_data$disp, my_data$mpg, type = "l", xaxt="n")
axis(1, at=xpos, labels=sprintf("%.2fKb", xpos/10))
With this we get what we need:
Now we try to get the exact same on shiny with ggvis:
library(shiny)
library(ggvis)
mtcars
ui <- pageWithSidebar( div(),
sidebarPanel(uiOutput("plot_ui"),width=2),
mainPanel(ggvisOutput("plot"))
)
server <- function(input, output, session) {
mtc <- reactive({
my_data <- mtcars[1:10, ]
# Do some sorting/ordering if you want (here sorted by disp)
my_data <- my_data[order(my_data$disp), ]
my_labels <- c(as.character(paste0((my_data$disp)/1000, "Kb")))
y <- my_data$mpg
x <- factor(c(my_data$disp), labels = c(unique(my_labels)))
data.frame(x = x, y = y)
})
mtc %>%
ggvis(~x,~y ) %>%
layer_lines() %>%
add_axis("x", properties=axis_props(labels=list(fontSize = 10))) %>%
bind_shiny("plot", "plot_ui")
}
shinyApp(ui = ui, server = server)
As we highlight in red, the distances are not correct. So how can we have on shiny the same plot we have on plain R with axis(label=... ?


