4

I'm trying to arrange two ggplot object converted to a plotly object and use one common legend. But the legend is somehow doubled:

enter image description here

df1 <- read.table(text = "group   x     y   
              group1 -0.212201  0.358867
              group2 -0.279756 -0.126194
              group3  0.186860 -0.203273
              group4  0.417117 -0.002592
              group1 -0.212201  0.358867
              group2 -0.279756 -0.126194
              group3  0.186860 -0.203273
              group4  0.186860 -0.203273", header = TRUE)

df2 <- read.table(text = "group   x     y   
              group1  0.211826 -0.306214
              group2 -0.072626  0.104988
              group3 -0.072626  0.104988
              group4 -0.072626  0.104988
              group1  0.211826 -0.306214
              group2 -0.072626  0.104988
              group3 -0.072626  0.104988
              group4 -0.072626  0.104988", header = TRUE)
library(dplyr)
library(ggplot2)
library(plotly)

p1 <- ggplot(df1, aes(x = x, y = y, colour = group)) +     
  geom_point(position = position_jitter(w = 0.04, h = 0.02), size = 1.8)

p2 <- ggplot(df2, aes(x = x, y = y, colour = group)) + 
  geom_point(position = position_jitter(w = 0.04, h = 0.02), size = 1.8)

subplot(ggplotly(p1), ggplotly(p2), nrows = 1)

I tried

 subplot(ggplotly(p1), ggplotly(p2), nrows = 1) %>% layout(showlegend = FALSE)

but the whole legend just vanishes

eipi10
  • 91,525
  • 24
  • 209
  • 285
schlusie
  • 1,907
  • 2
  • 20
  • 26

1 Answers1

6

I wasn't able to fix the double legend with two separate plots, but you can combine the two data frames to make a single faceted plot. Regardless of the legend issue, using a single data frame with faceting seems like a more natural approach, given that the grouping variable is the same in each data frame. In the example below, I've removed the facet strips in order to match your example, but you can keep them by deleting the theme statement.

p = ggplot(bind_rows(df1 %>% mutate(df="df1"), df2 %>% mutate(df="df2")),
       aes(x = x, y = y, colour = group)) +
  geom_point(position = position_jitter(w = 0.04, h = 0.02), size = 1.8) +
  facet_wrap(~ df, scales="free") +
  theme(strip.text=element_blank())

ggplotly(p)

enter image description here

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • thanks for the answer. Do you know how to select the same region in both plots, when a region is selected? – schlusie Nov 08 '16 at 21:37
  • Do you mean that you want the ranges of the x and y axes to be the same? If so, there are a number options. (1) delete `scales="free"`. (2) Replace `facet_wrap(~ df, scales="free") +` with `facet_grid(. ~ df) +`. (3) Set the ranges manually with, for example, `scale_y_continuous(limits=c(-0.2,0.3))` and similarly for the x-axis. (4) Set the ranges manually with `coord_cartesian(xlim=c(-0.3, 0.4), ylim=c(-0.2, 0.4))`. – eipi10 Nov 08 '16 at 22:12
  • Thanks. But what I wanted is in the plotly object: I can zoom in by drawing a rectangle or a certain region. Then just the axis on the selected plot adapts. But I want both axis to adapt. – schlusie Nov 09 '16 at 13:39