6

Hi I'm a little confused with the scales in ggvis. I'm trying to do two things: one is have a log scale (the equivalent of log="x" in plot()). I'm also looking for the equivalent of xlim=c(). In both cases, the code below is not giving the expected results.

# install.packages("ggvis", dependencies = TRUE)
library(ggvis)
df <- data.frame(a=c(1, 2, 3, 1000, 10000), b=c(0.1069, 0.0278, 0.0860, 15.5640, 30.1745))
df %>% ggvis(~a, ~b)
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="log")

Notice that with trans="log", all dots are on the left of the plot and the scale disappears.

Next, I want to restrict the plot to certain values. I could subset the data frame but I'm looking to have the equivalent of xlim from plot().

df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="linear", domain=c(10, 40))

This is giving even weirder results, so I'm guessing I might be misinterpreting what domain does.

Thanks for your help!

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
nico S
  • 81
  • 4

3 Answers3

3

I've encountered the same problem that you've mentioned.

Apparently, the developer of ggvis noticed this bug as well. This is currently marked as an issue. You can find the issue here: https://github.com/rstudio/ggvis/issues/230

In the context of your question:

# install.packages("ggvis", dependencies = TRUE)
library(ggvis)
df <- data.frame(a=c(1, 2, 3, 1000, 10000), b=c(0.1069, 0.0278, 0.0860, 15.5640, 30.1745))
df %>% ggvis(~a, ~b)

# Points will disapper
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="log")
# Should work
df %>% ggvis(~a, ~b) %>% scale_numeric("x", trans="log", expand=0)

However, you may notice that after the transformation, the spacing between ticks don't appear to be uniform. But at least the dots are rendered correctly.

Xin Yin
  • 2,896
  • 21
  • 20
1

I ran into the same issue and noticed that as soon as I removed the 0 data points from my data (of which log() cannot be computed) everything started to work fine.

pawel
  • 11
  • 1
0

This is strange, I've now tried a lot of things with your data, but can't find the problem.

The I tested it on the Violent Crime Rates by US State (see help(USArrests)) and it worked like a charm.

data(USArrests)
# str(USArrests)
p <- USArrests %>% ggvis(~ Murder, ~ Rape) %>% layer_points()
p %>% scale_numeric("y", trans = "log")

This is not an answer, simply to share this with you.

Eric Fail
  • 8,191
  • 8
  • 72
  • 128
  • Agreed! Using the pre-baked datasets, everything works, but not when I provide my own data! – nico S Aug 02 '14 at 18:27
  • I also tried extending your example to match the dimensions of `mtcars`, but that didn't help either. Tried identifying any differences using `str`, but none were found. – Eric Fail Aug 02 '14 at 18:31