Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I label plot tick marks using ggvis

Tags:

r

ggvis

I am trying to change the tick labels in a ggvis plot. My data points are x = c(1,2,3) and y = c(1,2,3). However, the following code results in tick labels which make no sense at all!

library(dplyr)
library(ggvis)
data.frame(x = c(1,2,3), y = c(1,2,3) ) %>%
  ggvis(~x,~y ) %>%
  layer_lines() %>%
  add_axis("x", properties=axis_props(
    labels=list(angle=90, fontSize = 10, text = c("one","two","three"  )      )
   )
   )

gives:

enter image description here

I imagine that I have to format the ticks as well, or at least tell ggvis which ticks to label?

like image 254
Alex Avatar asked Dec 19 '22 12:12

Alex


1 Answers

You are using the text property in a wrong manner.

ggvis itself only performs data binding and processing in R. It then "translates" the ggvis object into the visualization grammar defined by vega.js, which builds on top of d3.js to perform the actual rendering.

So, add_axis is simply a thin wrapper in ggvis for defining Axes properties in vega.js. You can find more about this using this document.

You can pretty much see that the arguments you pass to add_axis() function have a one-to-one mapping to the JSON specifications you would specify in vega.js. Therefore, properties=axis_props(...) in ggvis maps to the properties of Axis properties (I know it may sound confusing. But click above link and you would see the hierarchy there).

The properties parameter will define, I'm quoting the document here,

Optional mark property definitions for custom axis styling. The input object can include sub-objects for ticks (both major and minor), majorTicks, minorTicks, labels and axis (for the axis line).

Hence, the properties parameter is only supposed to change the styling, including the styling of labels, ticks, and the axis lines.

Your code :

properties=axis_props(labels=list(angle=90, fontSize = 10, 
                      text = c("one","two","three"))

can be abstracted as

properties=axis_props(labels=list(...))

which, based on our above discussion, is manipulating the styling of the axis labels. Each label is simply a SVG <text> element, whose tweakable properties can be found in this document. And by modifying the styling of the axis label, you change the styling of ALL x-axis labels.

In the end, it means that by specifying text = c("one","two","three"), you end up with manually setting every x-axis tick label to an array ["one", "two", "three"], which is joined to form the string one,two,three.


Solution

By default, ggvis will determine axis properties for you, including

  • How many ticks on the axis?
  • What are the values of the ticks?

This is how, without specifying add_axis("y", ...), you end up with a nicely render y-axis. But you can manually override the ticks by specifying the values property in add_axis() function.

For example,

data.frame(x = c(1,2,3), y = c(1,2,3) ) %>%
  ggvis(~x,~y ) %>%
  layer_lines() %>%
  add_axis("x",
           value=c(1, 2, 3),
           properties=axis_props(
    labels=list(angle=90, fontSize = 10)))

This gives us:

enter image description here

Closer, but not there yet, because the labels are numbers, not the strings you want.

Finally, in order to change the labels, we can simply change the data on the X-axis from numerics to factors, like:

x <- factor(c(1,2,3), labels=c("one", "two", "three"))
data.frame(x = x, y = c(1,2,3) ) %>%
  ggvis(~x,~y ) %>%
  layer_lines() %>%
  add_axis("x", values=x, 
           properties=axis_props(labels=list(angle=90, fontSize = 10)))

which will give you enter image description here

like image 169
Xin Yin Avatar answered Jan 08 '23 05:01

Xin Yin