3

I'm trying to add legends with arbitrary text in a ggvis plot using data from different dataframes. I have tried using add_legend() but I have not idea about what parameters to use. Using plot() is very simple using the legend() function but it has been very hard to find a way to do it using ggvis()

Here is a simple example of what I have using plot():

df1 = data.frame(x = sample(1:10), y = sample(1:10))
df2 = data.frame(x = 1:10, y = 1:10)
df3 = data.frame(x = 1:10, y = sqrt(1:10))

plot(df1)
lines(df2$x, df2$y, col = "red")
lines(df3$x, df3$y, col = "green")
legend("topleft", c("Data 2","Data 3"), lty = 1, col = c("red","green"))

Now, using ggvis() I can plot the points and the lines from different datasets but I can not find a way to put the legends using add_legend(), Here is the code using ggvis():

df1 %>% ggvis(x=~x,y=~y) %>% layer_points() %>% 
layer_paths(x=~x,y=~y,data = df2, stroke := "red") %>% 
layer_paths(x=~x,y=~y,data = df3, stroke := "green") 

I will really appreciate any help.

Thank you.

Edited:

This a sample code using only one data frame and plot()

df = data.frame(x = sample(1:10), y = sample(1:10), x2 = 1:10, y2 = 1:10, y3 = sqrt(1:10) )
plot(df[,c("x","y")])
lines(df$x2, df$y2, col = "red")
lines(df$x2, df$y3, col = "green")
legend("topleft", c("Data 2","Data 3"), lty = 1, col = c("red","green"))
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
Geovany
  • 5,389
  • 21
  • 37
  • I don't know if you can do this with two different data sets. It would make more sense though to make one data frame from the two and then melt it. You would get exactly what you want this way. If that would be acceptable for you then I can provide a solution. – LyzandeR Jun 20 '15 at 14:57
  • Yes, it is OK for me to have all the data in one data frame. Thank you. – Geovany Jun 21 '15 at 03:19

1 Answers1

3

So, what I came up with, is the following, which works:

#add an id column for df2 and df3 and then rbind
df2$id <- 1
df3$id <- 2
df4 <- rbind(df2,df3)
#turn id into a factor
df4$id <- factor(df4$id)

#then plot df4 using the stroke=~id argument
#then plot the legend
#and finally add df1 with a separate data
df4 %>% ggvis(x=~x,y=~y,stroke=~id) %>% layer_lines() %>%
        add_legend('stroke', orient="left") %>%
        layer_points(x=~x,y=~y,data = df1,stroke:='black') 

And it works:

enter image description here

If you would like to move the legend to a position inside the plot then you need to try this:

df4 %>% ggvis(x=~x,y=~y,stroke=~id) %>% layer_lines() %>%
  #make sure you use add relative scales
  add_relative_scales() %>%
  #values for x and y need to be between 0 and 1
  #e.g for the x-axis 0 is the at far-most left point and 1 at the far-right 
  add_legend("stroke", title = "Cylinders",
             properties = legend_props(
               legend = list(
                 x = scaled_value("x_rel", 0.1),
                 y = scaled_value("y_rel", 1)
               ))) %>%
  layer_points(x=~x,y=~y,data = df1,stroke:='black') 

And the output:

enter image description here

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • 1
    I would like to see this kind of examples on the documentation of ggvis. The official page (Rstudio) barely provides a few examples. Can you recommend me a good source for learning more about ggvis? Thank you. – Geovany Jun 21 '15 at 23:59
  • 2
    Yeah I know. The documentation is still poor unfortunately. It is probably because the developers have a lot on their heads at the moment, but it will get better. There is a documentation [here](http://ggvis.rstudio.com/ggvis-basics.html) and then I usually use `?`. For this example I just had a look at `?add_legend` and then tried out the examples and tried out lots of different arguments (that initially crashed a lot) until I got it right. There is no other source I 'm afraid, yet. – LyzandeR Jun 22 '15 at 00:04
  • Thank you for the advice :) – Geovany Jun 22 '15 at 00:13