Jonathan H Jonathan H - 2 months ago 5x
R Question

ggvis - custom non-numeric y-axis labels showing as 'NaN'

EDIT: Sample code updated with more complex example

I have a data frame with (x, y) values and a name for each pair, and I'm plotting it in a ggvis scatterplot. SO users already pointed me to this post (How do I label plot tick marks using ggvis) and I updated my code to reflect what I saw there by using

to create the list of custom y-axis labels
which is fed to
values =
under the
statement. Here is the new code:


df <- data.frame(x = 1:15,
y = c(, 15),, 15),, 15)))

ylabels_ <- factor(c(1,4,7), labels = stri_rand_strings(3, 7))

df %>%
ggvis(~x, ~y) %>%
add_axis("y", values = ylabels_)

My axis labels are still coming back with
instead of the factors. Am I missing something?

SOLUTION EDIT: Here is my code that I finally got to work. Note I changed the x values to be staggered so I could distinguish between the three lines.


df <- data.frame(x = c(seq(1, length.out = 15, by = 3),
seq(2, length.out = 15, by = 3),
seq(3, length.out = 15, by = 3)),
y = factor(c(, 15),, 15),, 15)), labels = c("one", "four", "seven"),
ordered = T))

df %>%
ggvis(~x, ~y) %>%
layer_points() %>%
scale_ordinal("y", domain = c("seven", "four", "one")

I don't understand why in the
domain =
portion I had to reverse the order of the factors for them to show in the correct order. If anyone has any insight on that, it would be very helpful!


Notice that in the linked answer, the new factor variable is used as the y variable. You can use it for your values, as well, but it's not necessarily needed.

Making the new factor based on y but using name as the labels is to get the name variable in the correct order for plotting.

df$ylabels_ <- factor(df$y, labels = stri_rand_strings(3, 7))

Because you are now working with a factor variable on the y axis, layer_points needs to be used explicitly.

df %>%
    ggvis(~x, ~ylabels_) %>% 

Note that this whole approach will only work if the original y data are integers.