rstudio / ggvis

Interactive grammar of graphics for R
Other
715 stars 173 forks source link

legends render on top of each other #125

Open ijlyttle opened 10 years ago

ijlyttle commented 10 years ago

I didn't see this in my quick scan of open issues:

It seems that each legend wants the same top-left attachment point within the view, so end up being printed on top of each other.

Here's an example:

ggvis(mtcars, props(x= ~mpg, y= ~disp, fill= ~factor(cyl), size= ~hp)) + 
  layer_point()

Here's the session info (probably time to update to the new version for me):

> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggvis_0.1.0.99    devtools_1.4.1.99 ggplot2_0.9.3.1   stringr_0.6.2    
[5] reshape2_1.2.2    lubridate_1.3.3   magrittr_1.0.0    dplyr_0.1.2      
[9] knitr_1.5.22     

loaded via a namespace (and not attached):
 [1] assertthat_0.1     bitops_1.0-6       caTools_1.16       colorspace_1.2-4  
 [5] dichromat_2.0-0    digest_0.6.4       evaluate_0.5.1     formatR_0.10      
 [9] grid_3.0.2         gtable_0.1.2       httpuv_1.2.3       httr_0.2          
[13] labeling_0.2       MASS_7.3-29        memoise_0.1        munsell_0.4.2     
[17] parallel_3.0.2     plyr_1.8.1         proto_0.3-10       RColorBrewer_1.0-5
[21] Rcpp_0.11.0        RCurl_1.95-4.1     RJSONIO_1.0-3      scales_0.2.3      
[25] shiny_0.9.1        tools_3.0.2        whisker_0.3-2      xtable_1.7-1   

Thanks!

ijlyttle commented 10 years ago

I see that this issue is alluded-to in the axes-legends vignette, and I found a workaround for the time-being:

ggvis(mtcars, props(x= ~mpg, y= ~disp, fill= ~factor(cyl), size= ~hp)) + 
  layer_point() + 
  guide_legend("size", orient = "left")

I'll leave it to Hadley et al. to decide if this should remain an issue.

I'll take my question on other workarounds to the google group.

wch commented 10 years ago

It looks like there's a bug in creating the legend section of the spec. This should work, but doesn't: ggvis(mtcars, props(x =~ mpg, y =~ disp, fill =~ factor(cyl), size =~ hp)) + layer_point() + guide_legend("size", properties = list(legend = props(y := 150)))

The problem is that in the spec, there should be a section that looks like this:

"legends" : [
  {
    "size" : "size",
    "orient" : "right",
    "properties" : {
      "legend" : {
        "y" : {
          "value" : 100
        }
      }
    },
    "title" : "hp"
  },
 ...
]

But instead the "properties" subsection looks like this, with an extra "update" in there:

  "properties" : {
    "legend" : {
      "update" : {
        "y" : {
          "value" : 150
        }
      }
    }
  }
ijlyttle commented 10 years ago

Hi Winston,

It looks like you've got the issue isolated.

Thanks,

Ian

hadley commented 10 years ago

Same as #145

wch commented 10 years ago

Updated example:

mtcars %>% ggvis(x= ~mpg, y= ~disp, fill= ~factor(cyl), size= ~hp) %>% 
  layer_points()

It would be nice to be able to automatically position the legends so they don't overlap. I suspect we won't be able to position them exactly, but that we'll have to use a heuristic and hope that there's no overlap.

Here's how to manually position the legend. It's way too verbose as-is:

mtcars %>% ggvis(x= ~mpg, y= ~disp, fill= ~factor(cyl), size= ~hp) %>% 
  layer_points() %>%
  add_legend(size = "size", properties = legend_props(legend = list(y = 100)))
yingchen69 commented 9 years ago

Hi, I tried a little change, but had no success

mtcars %>% 
  ggvis(x= ~mpg, y= ~disp, fill= ~factor(cyl)) %>% 
  layer_points(shape= ~factor(gear)) %>%
  group_by(gear) %>%
  layer_model_predictions(model = "lm", strokeDash = ~factor(gear)) %>%
  add_legend(shape = "shape", properties = legend_props(legend = list(y = 100)))

Instead,

        Guessing formula = disp ~ mpg
        Error in add_legend(`mtcars %>% ggvis(x = ~mpg, y = ~disp, fill = ~factor(cyl)) %>%           layer_points(shape = ~factor(gear)) %>% group_by(gear) %>%     layer_model_predictions(model =  "lm", strokeDash = ~factor(gear))`,  : 
        unused argument (shape = "shape")

How can I fix this?

Thanks a lot,

Ying

ijlyttle commented 9 years ago

Hi Ying,

This may just be a matter of a misnamed arguement in add_legend(): shape -> scales

Does this work for you?

mtcars %>% 
  ggvis(x= ~mpg, y= ~disp, fill= ~factor(cyl)) %>% 
  layer_points(shape= ~factor(gear)) %>%
  group_by(gear) %>%
  layer_model_predictions(model = "lm", strokeDash = ~factor(gear)) %>%
  add_legend(scales = "shape", properties = legend_props(legend = list(y = 100)))
yingchen69 commented 9 years ago

Hi Ian, it works! Thanks a lot for the help!

Ying

yingchen69 commented 9 years ago

Hi,

Sorry I got another issue. This method seems not work if I have add_tooltips() in the same ggvis plot.

mtcars %>% 
  ggvis(x= ~mpg, y= ~disp, fill= ~factor(hp)) %>% 
  layer_points(shape= ~factor(gear)) %>%
  add_tooltip(function(df) df$disp) %>%
  group_by(gear) %>%
  layer_model_predictions(model = "lm", strokeDash = ~factor(gear)) %>%
  add_legend(scales = "shape", properties = legend_props(legend = list(y = 200)))

It works for the first second, then the bottom legend jumps to top and overlaps with the top legends. One more trick?

Thanks a lot,

Ying

ijlyttle commented 9 years ago

This is more of a workaround than a fix.

Try adding set_options(duration = 0) to the end of your ggvis pipe.

yingchen69 commented 9 years ago

Hi Ian, thanks a lot. It works!

Ying

justacec commented 9 years ago

A was looking at this issue because I was running into it as well. Although I see the fix to manually place the y position of the legend, how do you know what the y value should be. My legend can shrink and grow. Is it possible to distribute the legends horizontally all cascading from the top? That would leave a lot of undesirable white space but it would avoid the issue of manual placement.

voxnonecho commented 9 years ago

For reference, StackOverflow user experiencing the same problem: http://stackoverflow.com/questions/30916337/legends-on-ggvis-graph-are-overlaping-when-using-tooltip/