结合图形与绘图

时间:2019-07-09 10:32:17

标签: r ggplot2 plotly

我正在尝试使用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)

enter image description here

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

2 个答案:

答案 0 :(得分:0)

如果您只是想这样做,则不使用密谋但结果相同:

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

答案 1 :(得分:0)

我也不确定您要干什么。 “传说小组”将有助于摆脱重复的传说。另外,您可以将标题添加到带有布局的整个子图中(来自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