每个方面下的多个值 - GGplot

Multiple values under each facet - GGplot

我正在尝试并排绘制每个因子水平的散点图。

library(ggplot2)
library(plotly)
dput(df)
structure(list(business_date = structure(c(17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 17245, 
17245, 17245), class = "Date"), token_id = c(1.85075e+19, 1.85075e+19, 
1.70088e+19, 1.44736e+19, 1.70234e+19, 1.70234e+19, 1.28442e+19, 
1.21512e+19, 1.21512e+19, 1.21512e+19, 1.50179e+19, 1.50179e+19, 
1.50179e+19, 1.24174e+19, 1.24174e+19, 1.2993e+19, 1.2993e+19, 
1.2993e+19, 1.2993e+19, 1.98549e+19, 1.98549e+19, 1.98549e+19, 
1.98549e+19, 1.98549e+19, 1.98549e+19, 1.98549e+19, 1.98549e+19, 
1.18468e+19, 1.18468e+19, 1.18468e+19, 1.18468e+19, 1.18468e+19, 
1.18468e+19, 1.66233e+19, 1.21387e+19, 1.21387e+19, 1.9162e+19, 
1.40385e+19, 1.40385e+19, 1.40385e+19, 1.40385e+19, 1.69103e+19, 
1.57387e+19, 1.57387e+19, 1.57387e+19, 1.57387e+19, 1.40552e+19, 
1.43084e+19, 1.15233e+19, 1.15233e+19), entry_sp_id = structure(c(4L, 
8L, 8L, 6L, 8L, 8L, 4L, 8L, 8L, 6L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 
6L, 4L, 6L, 6L, 4L, 6L, 8L, 4L, 4L, 8L, 8L, 6L, 6L, 1L, 4L, 4L, 
4L), .Label = c("GA", "LT", "NA", "SB", "ST", "SM", "SMN", "TT"
), class = "factor"), exit_sp_id = structure(c(4L, 8L, 8L, 6L, 
8L, 8L, 4L, 8L, 8L, 6L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 
4L, 4L, 4L, 4L, 4L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 4L, 6L, 
6L, 4L, 6L, 8L, 4L, 4L, 8L, 8L, 6L, 6L, 1L, 4L, 4L, 4L), .Label = c("GA", 
"LT", "NA", "SB", "ST", "SM", 
"SMN", "TT"), class = "factor"), dist = c(1.5, 
1.9, 5.1, 0.5, 1.1, 1.6, 1.9, 2, 1.9, 0.9, 0.6, 0.9, 1.6, 4.1, 
4.2, 1.1, 0.5, 0.8, 3.9, 3.2, 0.9, 10.1, 9.1, 1.2, 7.4, 0.4, 
0.9, 1.7, 2.5, 5.6, 2, 1.2, 1.7, 1.2, 1.8, 9.2, 0.8, 2.4, 7.8, 
2.5, 11, 1.2, 3.1, 5.5, 9.2, 3.5, 2.2, 0.6, 2.9, 1.1), Travel_Time = c(5, 
8, 23, 2, 5, 8, 9, 7, 9, 3, 1, 2, 5, 13, 12, 3, 1, 2, 21, 12, 
3, 28, 34, 3, 18, 2, 6, 5, 10, 27, 9, 8, 5, 7, 6, 26, 2, 3, 18, 
14, 35, 3, 14, 21, 44, 18, 10, 2, 18, 8)), .Names = c("business_date", 
"token_id", "entry_sp_id", "exit_sp_id", "dist", "Travel_Time"
), row.names = c(10813L, 10816L, 10821L, 10830L, 10839L, 10842L, 
10846L, 10848L, 10849L, 10851L, 10856L, 10857L, 10858L, 10860L, 
10862L, 10863L, 10866L, 10867L, 10868L, 10870L, 10871L, 10873L, 
10875L, 10876L, 10877L, 10878L, 10879L, 10885L, 10887L, 10888L, 
10889L, 10890L, 10892L, 10893L, 10898L, 10899L, 10901L, 10904L, 
10905L, 10907L, 10909L, 10914L, 10916L, 10917L, 10918L, 10919L, 
10922L, 10927L, 10930L, 10931L), class = "data.frame")

> p5 <- ggplot(df, aes(x=df$dist, y=df$Travel_Time)) +
+ geom_point(aes(colour=df$entry_sp_id)) +
+ geom_smooth(method="lm") + facet_grid(. ~ df$entry_sp_id)
> ggplotly(p5)

但是散点图 - 每个方面内的值显示其他因素水平的值(每个方面下应该只有一个因素水平值 - 每个方面下一种颜色和一种形状)。

您必须删除 $,因为在这种情况下它是多余的。您已经在 ggplot 中通过 data=df 定义了来源,因此无需使用 $ 调用列。此外,因为你用 $ 调用它,它作为向量出现在 ggplot 中,并丢失了哪些值属于哪个 entry_sp_id 的信息,这会导致你的问题。

 p5 <- ggplot(df, aes(x=dist, y=Travel_Time)) +
 geom_point(aes(colour=entry_sp_id)) +
 geom_smooth(method="lm") +facet_grid(~entry_sp_id)
 ggplotly(p5)

编辑:拼写:)