Enhancing ggvis axes

自作多情 提交于 2019-12-09 01:19:37

问题


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:



来源:https://stackoverflow.com/questions/27479765/enhancing-ggvis-axes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!