基于列标题的绘图

时间:2018-04-11 19:10:06

标签: r ggplot2

我有一种感觉这可能是一个基本问题,但我很难在任何地方找到答案..... 我有一个数据集,它以日期列开头,然后是每个日期的多列数据

Date                `Day Name`  Player.Name Min_1 Min_2 Min_3 Min_4 Min_5 
Min_6 Min_7 Min_8 Min_9
<dttm>              <chr>       <chr>       <dbl> <dbl> <dbl> <dbl> <dbl> 
<dbl> <dbl> <dbl> <dbl>
1 2018-04-05 00:00:00 Attack_3G   Adams Josh   142.  131.  120.  118. 117.  
114.  112.  110.  106. 
2 2018-04-03 00:00:00 Defence_3G  Adams Josh   141.  136.  119.  107. 114.  
110.  106.  103.  102. 
3 2018-04-07 00:00:00 Newcastle_H Adams Josh   164.  134.  125.  131. 123.  
122.  123.  118.  116. 
4 2018-04-09 00:00:00 Rugby_Games Adams Josh   143.  119.  106.  102.  99.1  
96.1  95.8  93.3  93.1
5 2018-04-10 00:00:00 Defence_3G  Adams Josh   151.  125.  118.  115. 105.  
105.  104.   98.3  99.2

我正在寻找使用ggplot生成与此grapg(使用excel制作)类似的东西,但我正在努力制作它 e

我受到了https://github.com/dylanjm/tidy_tuesday/blob/master/tidy_02/tidy_02.pdf的启发 但我知道我这么做很长的路要走

编辑:

dput(DataAdams)
structure(list(Date = structure(c(1522886400, 1522713600, 1523059200, 
1523232000, 1523318400), class = c("POSIXct", "POSIXt"), tzone = "UTC"), 
`Day Name` = c("Attack_3G", "Defence_3G", "Newcastle_H", 
"Rugby_Games", "Defence_3G"), Player.Name = c("Adams Josh", 
"Adams Josh", "Adams Josh", "Adams Josh", "Adams Josh"), 
Min_1 = c(142.1, 141, 164.4085833, 143.210266666667, 151.231866666667
), Min_2 = c(130.6, 136, 134.1398417, 118.640983333333, 124.572875
), Min_3 = c(120.3, 119, 125.4481778, 105.878772222222, 117.834138888889
), Min_4 = c(118, 107, 130.7162417, 102.362020833333, 115.113870833333
), Min_5 = c(116.7, 114, 123.2527933, 99.13397, 105.461176666667
), Min_6 = c(114.5, 109.91, 122.4857556, 96.0797777777778, 
104.625694444444), Min_7 = c(112.5, 106.33, 122.9992643, 
95.7929095238095, 103.714069047619), Min_8 = c(109.6, 103.25, 
117.8601313, 93.2561125, 98.3237854166667), Min_9 = c(105.8, 
101.79, 115.9276407, 93.0925740740741, 99.2409627156372)), .Names = c("Date", 
"Day Name", "Player.Name", "Min_1", "Min_2", "Min_3", "Min_4", 
"Min_5", "Min_6", "Min_7", "Min_8", "Min_9"), row.names = c(NA, 
-5L), class = c("tbl_df", "tbl", "data.frame"))
> 

1 个答案:

答案 0 :(得分:1)

您的数据集不会生成示例图。有两个“Defence_3G”行。所以为了简化,我将忽略最后一行。

library(tidyverse)
adams <- DataAdams %>% slice(1:4)

所以第一个问题是您的数据不常用Excel电子表格常见的方式。 “变量”Min_1等实际上是数据值。无论如何,我们需要将数据放入“长”格式,如下所示。 gather来自tidyr,它是作为核心tidyverse的一部分加载的。

adams_long <- adams %>% 
  gather(key, value, -Player.Name, -Date, -`Day Name`)

查看此数据框以查看其外观。现在它已准备好ggplot

adams_long %>% 
  ggplot(aes(x=key, y=value, colour = `Day Name`)) + 
  geom_point() + 
  geom_line(aes(group=`Day Name`))

enter image description here

有许多方法可以调整外观和感觉。