-->

Enhancing ggvis axes

2020-03-06 02:56发布

问题:

I am looking for the best way to enhance a chart

library(dplyr)
library(ggvis)

df <- data.frame(Year=c(1954:2013), Count=rep(as.integer(c(1,3,4,2)),15))

df %>%
ggvis(~Year,~Count)

I would like to show only whole numbers in the y-axis and remove the thousand-comma in the x-axis

I have coerced both fields to factors with this hack

df %>%
 ggvis(~as.factor(Year),~as.factor(Count)) %>% 
 layer_points() %>%
 add_axis("y", title="Count") %>%
 add_axis("x", title="Year") %>%
 scale_ordinal("y", reverse=TRUE)

but now I am showing every year, rather than the more appropriate 5 year values shown before and amending the label properties only helps so much

Help much appreciated

回答1:

This can be done using the format= in the add_axis along with subdivide argument -

A subdivide = 0 means no minor ticks between major ticks (defined in values). The format='####' makes everything whole numbers.

 df %>%
  ggvis(~Year,~Count) %>%
  layer_points() %>%
  add_axis("x", title="Year",  format="####") %>%
  add_axis("y", subdivide = 0, values = seq(1, 4, by = 1), format='####')

which gives:



标签: r ggvis