0

Note, I am relatively knew to R and have learned a lot from questions and answers found here.

So, I am have a dataframe that has month and year currently as a character string in the form "yyyy-mm". The original data was by minutes and so I used the group_by function to take the average of the data. I noticed when using group_by I was received a PosIXlt error and thus made the column a character string.

That worked in order to do the group_by and I was able to get the averages by month. Now when I am trying to make a line graph using the Month_Yr column as my x-axis but am now not able to plot due to the characters. A sample of my df is:

 Month_Yr  GAP_Mean
   <chr>     <dbl>
  2006-12 1.9012951 
  2007-01 1.5460339
  2007-02 1.4010835
  2007-03 1.3186270

etc. I have several other columns and am trying to create all of them on one graph and found help via other posts and was able to graph when I just had months but the Month_Yr column is giving me trouble. When putting in the following code, I get the warning "geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?".

Month_Yr%>% gather(key, Watts, GAP_Mean, GAP_Diff) %>% ggplot(aes(x=Month_yr,y=Watts,colour=key))+geom_line()

I am hoping someone can help. As said, I am relatively new to R. I had to write a report using this data and threw it in Excel but now am trying to get more accustomed to the visualisation packages of R. Please let me know if you I have been unclear or you need any further info.

Joy Fernandes
  • 303
  • 3
  • 16
AshG
  • 1
  • Have you tried converting your Month_Yr column back to a date format and then plotting? – Alex P Jul 28 '17 at 17:57
  • I tried doing that but unless I am mistaken, I cannot do a date format without a day, correct? I stripped the date into now only characters in order to be able to do the group_by. Can I parse it and default the day to convert it back to a date and then show it only as month and year in the graph? – AshG Jul 28 '17 at 18:26
  • Add `group = key` inside your `aes()` call. Also be careful about case sensitivity, your sample data shows `Month_Yr`, but your plot code uses `Month_yr`. These should match exactly. – Gregor Thomas Jul 28 '17 at 18:28
  • Thank you @Gregor. That worked. My apologies in the duplicate post. I should have been more disciplined in my search for existing answers before posting. – AshG Jul 28 '17 at 18:38
  • No worries on the duplicate - this question now serves as a pointer to that one, making it that much easier to find. (But when you get an error message it's almost always worth searching for all or part of it. I just searched Stack Overflow for "[r] Each group consists of only one observation" and that was the top hit.) – Gregor Thomas Jul 28 '17 at 19:04
  • @AshG, `as.yearmon()` is an option. Glad you got the issue resolved. – Alex P Jul 28 '17 at 19:10

0 Answers0