I have a ggvis plot in a Shiny app with too many x axis labels (see https://i.stack.imgur.com/lLppD.png).
In my ggplot implementation I called a function mylabels that would skip most labels and only display every nth label:
scale_x_discrete(labels=mylabels)
where
mylabels <- function(x) {
y = c()
marks = seq(1, length(x), 7)
for (j in 1:length(x)) {
if (j %in% marks) {
y[j] = x[j]
}
else {
y[j] = ""
}
}
return (y)
}
This worked well in ggplot. I tried to implement the same solution in ggvis but to no success. Here is my Shiny and ggvis code:
plotdata <- reactive({
this_dataset <- input$dataset
all = read.csv(paste(this_dataset, ".csv",sep=""), header = T)
filtered = all[all$year == input$YearSelect,]
return(filtered)
})
plotdata %>% ggvis(~factor(Var1), ~nums) %>%
layer_bars(fill := ~color) %>%
add_axis("x", values=mylabels) %>%
bind_shiny("yearPlot")
When I call
values=mylabels
the function doesn't seem to get called at all and I get the default labels.
The function seems to get called if I write
values=mylabels()
but then I don't know how to pass the default labels as an argument to the function.
I tried
values=mylabels(plotdata$Var1)
but then I get
Error in plotdata$Var1 : object of type 'closure' is not subsettable
If anyone knows a better solution to skip labels, that would be great too. Thanks a lot.