Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Unify scale_info and scales #180

Merged
merged 20 commits into from
Jun 5, 2014
Merged

Unify scale_info and scales #180

merged 20 commits into from
Jun 5, 2014

Conversation

wch
Copy link
Contributor

@wch wch commented Jun 5, 2014

Fixes #34.

# for the property and name. If we get something like "blah", then use that
# for the name, but use prop_name for the property.
property <- prop_scale(prop, default_scale = propname_to_scale(prop_name))
if (property %in% valid_scales) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having to use list of valid scale names seems a bit strange

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was probably the hackiest part of the whole pull request. It was necessary for these to work:

# Using custom scale name for a known property (fill)
df <- data.frame(x = 1:5, y = 5:1, z = runif(5))
df %>% ggvis(x = ~x, y = ~x, fill = prop(~z, scale = "blah")) %>%
  layer_points()

# Use props like x2, y2
df %>% ggvis(x = ~x, y = ~x, x2 = ~x+1, y2 = ~x+1) %>%
  layer_rects()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you figure it out by inspect properties of the prop?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe... Here are some cases to consider:

# 1
ggvis(x = ~y)

# 2
ggvis(x2 = ~y)

# 3
ggvis(x = prop(~y, scale = "x"))

# 4
ggvis(x = prop(~y, scale = "blah"))

In each case the props will be something very roughly like this (stripped down for simplicity):

# 1
list(x = list(value = quote(y), scale = TRUE))

# 2
list(x2 = list(value = quote(y), scale = TRUE))

# 3
list(x = list(value = quote(y), scale = "x"))

# 4
list(x = list(value = quote(y), scale = "blah"))

In cases 1, 2, and 3, you want property="x", name="x", and label="y".

In case 4, you want property="x", name="blah", and label="y".

Also, this has made me realize that there's a bug in getting the labels.

Here are real, examples:

df <- data.frame(xvar = 1:5)
# 1
df %>% ggvis(x = ~xvar, y = 1) %>% layer_points()

# 2
df %>% ggvis(x = 1, x2 = ~xvar+0.5, y = ~xvar, y2 = 0.5) %>% layer_rects(fillOpacity := 0.2)

# 3
df %>% ggvis(x = prop(~xvar, scale = "x"), y = 1) %>% layer_points()

# 4
df %>% ggvis(x = prop(~xvar, scale = "blah"), y = 1) %>% layer_points() %>%
  add_guide_axis("x", "blah")

@hadley
Copy link
Member

hadley commented Jun 5, 2014

It's not clear that this actually makes the code easier to understand - in particular, I don't like this:

vis <- add_scale(vis, ggvis_scale("x", label = prop_name(cur_props(vis)$x.update)))

because it implies we have two types of scale objects - complete scales, and partial scales. Could we at least extract that out into its own function, e.g.:

vis <- set_scale_name(vis, "x", prop_name(cur_props(vis)$x.update))

It's also not clear why you needed to pull the scales functions apart into three pieces (internal, external and defaults). That makes the code much harder to follow.

@wch
Copy link
Contributor Author

wch commented Jun 5, 2014

There is a bit more complexity with the internal vs. external, but that's necessary because scale_info stuff is all gone now -- so that parallel data structure is gone. Instead of adding a scale_info object each time a mark is added, a scale object is added. But this requires the ability to add "sparse" scale objects, where a scale might only set a title, or set the domain, instead of setting all of the properties needed for a fully-fleshed-out, usable scale. The internal functions are for creating these sparse scale objects.

When the plot is built, the scale objects for a particular scale (like fill, or x) get collapsed down into a single scale object. So they're collapsed, and then defaults are applied, in case the collapsed scale object is still missing some required fields.

I agree that it makes sense to add a function for setting scale label, like set_scale_label.

@hadley
Copy link
Member

hadley commented Jun 5, 2014

How about only checking if properties are not null?

wch added a commit that referenced this pull request Jun 5, 2014
@wch wch merged commit 51f560d into master Jun 5, 2014
@wch wch deleted the unify-scales branch June 9, 2014 19:33
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better automatic detection for scales and axes
2 participants