NColl
NColl

Reputation: 757

Plotting Based on Column Headers

I have a feeling this may be a basic problem but I'm struggling to find an answer anywhere..... I have a dataset which begins with a date columns and then a number of columns of data for each date

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

I'm looking to produce something simliar to this grapg (made in excel) using ggplot but I'm struggling to produce it e

I have been slightly inspired by this https://github.com/dylanjm/tidy_tuesday/blob/master/tidy_02/tidy_02.pdf but I know I'm a long way doing that

Edit:

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"))
> 

Upvotes: 0

Views: 70

Answers (1)

ngm
ngm

Reputation: 2589

Your dataset would not produce the example plot. There are two "Defence_3G" rows. So to simplify I'll ignore the last row.

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

So the first issue is that your data is untidy in a way often seen with Excel spreadsheets. The "variables" Min_1 etc. are actually data values. Anyway, we need to put the data into "long" format, as follows. gather is from tidyr which is loaded as part of core tidyverse.

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

Have a look at this data frame to see what it looks like. Now it's ready for ggplot.

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

enter image description here

And there are lots of ways to tweak the look and feel.

Upvotes: 1

Related Questions