【问题标题】:building a ggplot facet grid for 4 columns after melt融化后为 4 列构建 ggplot facet 网格
【发布时间】:2019-10-04 12:25:29
【问题描述】:

我从一张更宽的桌子开始,融化后我把桌子变成了 4 列,如下所示:

      team       DATE     Rank        winning_times
     team1    20180925 place1           1646
     team2    20180925 place1            876
     team3    20180925 place1            601
     team4    20180925 place1            438
     team5    20180925 place1            321
     team1    20180925 place2           1546
     team2    20180925 place2            976
     team3    20180925 place2            501
     team4    20180925 place2            338
     team5    20180925 place2            421
     team1    20180925 place3           2546
     team2    20180925 place3            476
     team3    20180925 place3            501
     team4    20180925 place3            638
     team5    20180925 place3            121
     team1    20180926 place1           1046
     team2    20180926 place1            806
     team3    20180926 place1            61
     team4    20180926 place1            48
     team5    20180926 place1            31
     team1    20180925 place2           1446
     team2    20180925 place2            276
     team3    20180925 place2            201
     team4    20180925 place2            238
     team5    20180925 place2            221 

我的目标是构建一个绘图,其中 X 轴代表日期,y 轴代表获胜时间,并构建一个 facet_grid 或 facet_wrap。但是当我尝试做 facet_grid 我得到 ​​p>

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

这也是一个大型数据集,其中包含每个团队 30 天的数据,代表他们获得的每个位置以及获胜时间。

ggplot(data=df.m, 
       aes(x=factor(DATE), y=winning_times, 
           group=Rank,
           shape=team,
           color=team)) + 
  geom_line() + 
  geom_point() +
  scale_x_discrete("DATE") +
  scale_y_continuous("WiningTimes") + 
  facet_grid(Rank ~ team )

【问题讨论】:

  • 什么是DisplayedTimes?它不在您的示例数据框中。
  • 您会收到警告,因为对于每个团队来说,place3 的面板只有一个观察结果。因此geom_line 不能产生一行。

标签: r ggplot2 facet-wrap facet-grid


【解决方案1】:

如果我在您的代码中将 DisplayedTimes 更改为 winning_times,我可以创建以下绘图。

library(ggplot2)

ggplot(data=df.m, 
       aes(x = factor(DATE), y = winning_times, 
           group = Rank,
           shape = team,
           color = team)) + 
  geom_line() + 
  geom_point() +
  scale_x_discrete("DATE") +
  scale_y_continuous("WiningTimes") + 
  facet_grid(Rank ~ team)

这对我来说看起来不错。

更新

带有直方图的版本。

library(ggplot2)

df.m$DATE <- factor(df.m$DATE)

ggplot(data=df.m, 
       aes(x = winning_times, 
           fill = DATE,
           color = DATE)) + 
  geom_histogram(alpha = 0.5, position = "identity") +
  scale_x_continuous("WiningTimes") +
  facet_grid(Rank ~ team)

数据

df.m <- read.table(text = "       team       DATE     Rank        winning_times
     team1    20180925 place1           1646
     team2    20180925 place1            876
     team3    20180925 place1            601
     team4    20180925 place1            438
     team5    20180925 place1            321
     team1    20180925 place2           1546
     team2    20180925 place2            976
     team3    20180925 place2            501
     team4    20180925 place2            338
     team5    20180925 place2            421
     team1    20180925 place3           2546
     team2    20180925 place3            476
     team3    20180925 place3            501
     team4    20180925 place3            638
     team5    20180925 place3            121
     team1    20180926 place1           1046
     team2    20180926 place1            806
     team3    20180926 place1            61
     team4    20180926 place1            48
     team5    20180926 place1            31
     team1    20180925 place2           1446
     team2    20180925 place2            276
     team3    20180925 place2            201
     team4    20180925 place2            238
     team5    20180925 place2            221 ",
                  header = TRUE, stringsAsFactors = FALSE)

【讨论】:

  • 错误的变量更正了它。它的wining_times 可以做直方图吗?而不是点线
  • @add-semi-colons 是的,我们可以绘制直方图。请查看我的更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-12
相关资源
最近更新 更多