3

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!

Community
  • 1
  • 1
Jonathan H
  • 89
  • 9

2 Answers2

1

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_) %>% 
    layer_points()  

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

aosmith
  • 34,856
  • 9
  • 84
  • 118
  • I think the example I gave is too simple - I'm going to post a revised example – Jonathan H Aug 19 '16 at 00:46
  • @JonathanH If the issue is repeated y values, put the new variable in your dataset instead of making a stand-alone vector. But as per my note about continuous variables, the factor will lead to equal spacing instead of "continuous" spacing. – aosmith Aug 19 '16 at 01:19
  • 1
    I ended up making many more modifications to the code, but your answer helped get me to where I needed to be! – Jonathan H Aug 19 '16 at 01:27
0

Call me a fool, but I'm calling this a bug (which, BTW, is not answering your question):

## set up data
lab <- factor(1:15, labels=stri_rand_strings(15, 7))
val <- 1:15

## all works according to expectations on the "x" axis
data.frame(xx=lab, yy=val) %>%
    ggvis(~xx, ~yy) %>% 
    layer_points() %>%
    add_axis("x", values=lab,
             properties=axis_props(labels=list(angle=90, fontSize = 10)))

## nothing works according to expectations on the "y" axis
## attempt 1
data.frame(xx=val, yy=lab) %>%
    ggvis(~xx, ~yy) %>% 
    layer_points() %>%
    add_axis("y", values=lab,
             properties=axis_props(labels=list(angle= 0, fontSize = 10)))

## attempt 2
data.frame(xx=val, yy=as.numeric(val)) %>%
    ggvis(~xx, ~yy) %>% 
    layer_points() %>%
    add_axis("y", values=lab,
             properties=axis_props(labels=list(angle= 0, fontSize = 10)))
renato vitolo
  • 1,744
  • 11
  • 16
  • Not really sure what the issue is as you described it, but the solution I found that worked for me is above – Jonathan H Aug 19 '16 at 01:26