0

Now this isn't right. I have a data set that for each team should provide a time series line, one called xG, one called xGA.

Instead I get this monstrosity: enter image description here

The data TimeSeriesxGxGA for the plot looks like this. Here's the first 10 lines:

Team, Date, variable, value
Aston Villa, 2018-12-18, xG, 37.56
Birmingham City, 2018-12-18, xG, 34.30
Blackburn Rovers, 2018-12-18, xG, 33.55
Bolton Wanderers, 2018-12-18, xG, 19.575
Brentford, 2018-12-18, xG, 35.03
Bristol City, 2018-12-18, xG, 32.43
Derby County, 2018-12-18, xG, 27.73
Hull City, 2018-12-18, xG, 28.91
Ipswich Town, 2018-12-18, xG, 15.61
Leeds United, 2018-12-18, xG, 34.61

It goes on like that for 384 lines and xG and xGA are variables that all increase over time.

The plot call is like so:

ggplot(TimeSeriesxGxGA) +
  geom_line(aes(Date, value, colour = variable), size = 1) +
  ylim(0, 55)  +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, face="bold")) +
  labs(title = "xG vs xGA since mid-Dec",
       x = "", y = "")

Where am I going wrong? x and y must be right (Date and value). Surely, the colour of the lines is what should be distinguished by colour?

Just for reference, here's a plot of one team's xG and xGA. The lines should look like this:

enter image description here

Mr_Percy_Heat
  • 59
  • 1
  • 10
  • 2
    I'm confused as to why you are `gather`ing data that is already in a long format, rather than wide. If you had columns callen `xG` and `xGA`, then I could see gathering those. As it is, wouldn't you just map color to variable and facet by team? – joran Feb 14 '19 at 21:32
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. If you want to color by team, how do you want to tell the xG/xGa lines apart? – MrFlick Feb 14 '19 at 21:32
  • Using your original data frame try plotting: `aes(Date, value, colour = Team, group=variable)` – Dave2e Feb 14 '19 at 21:37
  • That doesn't work. It does a similar thing connecting points that shouldn't connect. I realise it would be a mess if all the lines were as they should be, that there'd be a lot of xG and xGA to look at, but I could at least filter teams out. Ultimately, I want to use it to compare pairs or groups of teams. – Mr_Percy_Heat Feb 14 '19 at 21:48
  • I can make it show one team's xG and xGA but I'd like to compare more than one on the same chart. – Mr_Percy_Heat Feb 14 '19 at 21:49
  • 1
    You need to be more clear as to what suggestion "doesn't work". Mapping color to variable and faceting by team will work, although you may have to also set the group to variable. – joran Feb 14 '19 at 21:52
  • I suspect the monstrosity in the beginning of the post uses `geom_line(aes(Date, value, colour = Team))`, but your data has multiple `value`s for each `Team` because you are tracking both xG and xGA. Try mapping both Team and variable to aesthetics, e.g. with `colour = Team, size = variable` inside your `aes()`. – Jon Spring Feb 15 '19 at 06:53

0 Answers0