我试图安排两个转换为情节对象的ggplot对象并使用一个常见的图例。但传说在某种程度上翻了一番:
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)
我试过
subplot(ggplotly(p1), ggplotly(p2), nrows = 1) %>% layout(showlegend = FALSE)
但是整个传奇只是消失了
答案 0 :(得分:6)
我无法使用两个单独的图来修复双重图例,但您可以将两个数据框组合在一起以制作单个图面。无论传说问题如何,考虑到每个数据框中的分组变量相同,使用具有分面的单个数据框似乎是更自然的方法。在下面的示例中,我删除了构面条以匹配您的示例,但您可以通过删除theme
语句来保留它们。
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)