Jonathan H - 3 months ago 8

R Question

**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()`

`ylabels_`

`values =`

`add_axis()`

`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`

`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 =`

Answer

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.

Source (Stackoverflow)

Comments