【问题标题】:Multiple values under each facet - GGplot每个方面下的多个值 - GGplot
【发布时间】:2017-05-16 10:18:23
【问题描述】:

我并不想为每个因子水平并排绘制散点图。

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)

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

【问题讨论】:

  • 从您的代码中删除所有df$。这应该可以解决问题。
  • 好的。你能解释一下为什么会这样吗?
  • 在aes中使用带有$的ggplot“错误方式”并获得意外输出,然后在没有$的情况下使用“正确方式”并获得正确的输出,现在问它为什么有效?
  • @zx8754 不知道所以想明白了。学习没有错。

标签: r ggplot2 plotly scatter-plot facet


【解决方案1】:

您必须删除 $ 因为在这种情况下它是多余的。您已经在 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)

编辑:拼写:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2019-01-17
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多