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 factor()
to create the list of custom y-axis labels ylabels_
which is fed to values =
under the add_axis()
statement. Here is the new code:
library(stringi)
require(ggvis)
set.seed(13)
df <- data.frame(x = 1:15,
y = c(rep.int(1, 15),
rep.int(4, 15),
rep.int(7, 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 NaN
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.
library(stringi)
require(ggvis)
set.seed(13)
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(rep.int(1, 15),
rep.int(4, 15),
rep.int(7, 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!