【问题标题】:Combine graphs with plotly将图表与 plotly 结合起来
【发布时间】:2019-07-09 10:32:17
【问题描述】:

我正在尝试使用 Iris 数据集将同一张图片上的两个图表与 plotly 和 gridExtra 包结合起来。您可以在下面看到代码和图片。

   #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)

在上图中,您可以看到两个图表,有两个标题和两个图例。所以我的意图是两个删除一个标题结束一个图例,所以有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: r ggplot2 plotly


    【解决方案1】:

    如果这就是你所追求的,不要使用情节但相同的结果:

    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)
    

    【讨论】:

      【解决方案2】:

      我也不确定你到底想要什么。 'legendgroup' 将有助于摆脱重复的传说。此外,您可以使用布局为整个子图添加标题(来自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")
      

      【讨论】:

        猜你喜欢
        • 2021-09-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-09
        • 2020-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多