0

I am trying with Iris dataset to combine two graphs on same pic with plotly and gridExtra package.Below you can see code and pic.

   #CODE
library(plotly)
library(ggplot2)
library(dplyr)
library(gridExtra)

    ggiris1 <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
    ggiris2 <- qplot(Petal.Length, Sepal.Length, data = iris, color = Species)

    ply1 <- ggplotly(ggiris1)
    ply2 <- ggplotly(ggiris2)

    subplot(ply1, ply2, nrows=1)

enter image description here

On upper pic you can see two graphs, with two titles and two legends.So my intention is two remove one title end one legend,so can anybody help me how to resolve this problem ?

silent_hunter
  • 2,224
  • 1
  • 12
  • 30

2 Answers2

0

Not using plotly but same result if that's all you're after:

library(reshape)
iris<-melt(iris,measure.vars = c("Petal.Width","Petal.Length"))

ggplot(data=iris, aes(x=value, y=Sepal.Length,color=Species))+
  geom_point()+
  facet_grid("variable",scales="free")+ #get rid of scales = free if you don't want different x scales
  ggtitle("Species")+
  theme(strip.placement = "outside",
        #strip.text = element_text(color = "transparent"), #use this if you don't want the facets labeled
        strip.background = element_rect(fill="white", colour="white",size=1))+
  labs(x=NULL,y=NULL)

enter image description here

CrunchyTopping
  • 803
  • 7
  • 17
0

I'm also not sure exactly what you're going for. 'legendgroup' will help get rid of the repeated legends. Also, you can add a title to the overall subplot with layout (from Title of Subplots in Plotly).

library(plotly)
library(dplyr)

pBase <- iris %>%
  plot_ly(color = ~Species,
          legendgroup = ~Species)

p1 <- pBase %>%
  add_markers(x = ~Petal.Width,
              y = ~Sepal.Length)

p2 <- pBase %>%
  add_markers(x = ~Petal.Length, 
              y = ~Sepal.Length,
              showlegend = FALSE)

subplot(p1, p2, shareY = TRUE) %>% layout(title ="Main title")

enter image description here

Andreas
  • 210
  • 2
  • 9