问题
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:
I imagine that I have to format the ticks as well, or at least tell ggvis
which ticks to label?
回答1:
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:
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
回答2:
Try repeating add_axis()
function for each label you wish for, like this:
library(dplyr)
library(ggvis)
data.frame(x = c(1,2,3), y = c(1,2,3) ) %>%
ggvis(~x,~y ) %>%
layer_lines() %>%
add_axis("x",values = c(1,1), properties=axis_props(
labels=list(angle=90, fontSize = 10, text = 'one', dx = 10))) %>%
add_axis("x",values = c(2,2), properties=axis_props(
labels=list(angle=90, fontSize = 10, text = 'two', dx 10))) %>%
add_axis("x",values = c(3,3), properties=axis_props(
labels=list(angle=90, fontSize = 10, text = 'three', dx = 10)))
I had data with too many categories so the factoring solution wouldn't work for me. Notice that I always called a vector with the same repeated number under values parameter. I don't know why but when I called single numbers render did not happened. Solution I found was to was to input the same number twice xD. Also dx argument can do good here, avoiding label-axis overlays:
If your variable has too many categories and you wish for only few labels this is a way to achieve custom labels.
来源:https://stackoverflow.com/questions/25235107/how-do-i-label-plot-tick-marks-using-ggvis