我有两个散点图,我想说明回归线是不同的。
在一个案例中,我正在考虑1995-2017年的所有数据。我想表明,奇数年(95,97)的平均值高于一般水平。
所有数据的数据框为
bayern
,只有奇数年的是
bayern_ohne_wm_em
对于,我使用plotly创建了两条回归线:
p1 <- plot_ly(bayern, x = ~Saison, color = I("black")) %>%
add_markers(y = ~Bayern, text = rownames(bayern), showlegend = FALSE) %>%
add_lines(y = ~fitted(loess(Bayern ~ Saison)),
line = list(color = '#07A4B5'),
name = "Realität", showlegend = TRUE)
p2 <- plot_ly(bayern_ohne_wm_em, x = ~Saison, color = I("black")) %>%
add_markers(y = ~Bayern, text = rownames(bayern_ohne_wm_em), showlegend = FALSE) %>%
add_lines(y = ~fitted(loess(Bayern ~ Saison)),
line = list(color = '#000000'),
name = "Nur Saisons ohne WM/EM zuvor", showlegend = TRUE)
subplot(p1, p2)
然而,我实际上希望两条线都在一个绘图中。
我可以通过将两个数据帧连接到
joint2
我做的。
该数据框如下所示:
Saison Bayern ohne_WM_EM
2017 81 81
2016 75 NA
...
我尝试以下方法:
plot_ly(joint2, x = ~Saison, color = I("black")) %>%
add_markers(y = ~Bayern, text = rownames(joint2), showlegend = FALSE) %>%
add_lines(y = ~fitted(loess(Bayern ~ Saison)),
line = list(color = '#07A4B5'),
name = "Realität", showlegend = TRUE)%>%
add_markers(y = ~ohne_WM_EM, text = rownames(joint2), showlegend = FALSE) %>%
add_lines(y = ~fitted(loess(ohne_WM_EM ~ Saison)),
line = list(color = '#000000'),
name = "Nur Saisons ohne WM/EM zuvor", showlegend = TRUE)
这给了我一个错误:
Error: Column `y` must be length 1 or 22, not 11
谢谢大家!
编辑:
下面是一些调试内容:
> dput(bayern)
structure(list(Saison = c(2016, 2015, 2014, 2013, 2012, 2011,
2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000,
1999, 1998, 1997, 1996, 1995), Bayern = c(82, 88, 79, 90, 91,
73, 65, 70, 67, 76, 60, 75, 77, 68, 75, 68, 63, 73, 78, 66, 71,
62)), .Names = c("Saison", "Bayern"), row.names = c(NA, -22L),
class = c("tbl_df", "tbl", "data.frame"))
> dput(joint2)
structure(list(Saison = c(2016, 2015, 2014, 2013, 2012, 2011,
2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000,
1999, 1998, 1997, 1996, 1995), Bayern = c(82, 88, 79, 90, 91,
73, 65, 70, 67, 76, 60, 75, 77, 68, 75, 68, 63, 73, 78, 66, 71,
62), ohne_WM_EM = c(NA, 88, NA, 90, NA, 73, NA, 70, NA, 76, NA,
75, NA, 68, NA, 68, NA, 73, NA, 66, NA, 62)), .Names = c("Saison",
"Bayern", "ohne_WM_EM"), row.names = c(NA, -22L), class = "data.frame")