Struggling with Multiple Line Plot and geom_line

I am trying to create a line graph with multiple lines and points. I am struggling with 2 problems.

  1. I can get the points to appear in a graph, but when I add the geom_line() function , I am told my x axis object, match_date, is not found.
  2. Sometimes when I plot the graph, it does not pick up my date format correctly.

Columns of my dataset include match_date, team, and Shots.

Thank you for taking your time to help me.

tourn_stats <- data.frame(match_date = c(2019-07-06, 2019-06-07, 2019-06-19, 2019-06-14, 2019-06-23, 2019-06-27, 2019-07-02, 2019-06-23, 2019-06-07, 2019-06-12, 2019-06-28, 2019-06-17),
                          team = c("England", "England", "England", "England", "England", "England", "England", "France", "France", "France", "France", "France"),
                          Shots = c(10, 16, 17, 17, 7, 16, 7, 14, 22, 19, 19, 19))

ggplot(tourn_stats)+geom_point(size = 3, mapping=aes(x=match_date, y=Shots, color = team))+
  xlab("Match Date")+
  ylab("Number of Shots")+
  geom_line(x = match_date, y = Shots)

  

Console Output:
ggplot(tourn_stats)+geom_point(size = 3, mapping=aes(x=match_date, y=Shots, color = team))+

  • xlab("Match Date")+
  • ylab("Number of Shots")+
  • geom_point(x = match_date, y = Shots)
    Error: object 'match_date' not found

Below are two ggplot calls that work with your data. Notice that I put quotes around the dates in the initial data and then converted the strings to dates. If you just 2019-07-06, you will get the number 2006, the result of the subtraction.
ggplot could not find the match_date column in geom_line() because match_date was not inside an aes() function. Shots would have caused the same problem.
The first ggplot() version has a black line and colored points. In the second version, both the lines and points are colored.

tourn_stats <- data.frame(match_date = c('2019-07-06', '2019-06-07', '2019-06-19', 
                                         '2019-06-14', '2019-06-23', '2019-06-27', 
                                         '2019-07-02', '2019-06-23', '2019-06-07', 
                                         '2019-06-12', '2019-06-28', '2019-06-17'),
                          team = c("England", "England", "England", "England", 
                                   "England", "England", "England", "France", 
                                   "France", "France", "France", "France"),
                          Shots = c(10, 16, 17, 17, 7, 16, 7, 14, 22, 19, 19, 19))
tourn_stats$match_date <- as.Date(tourn_stats$match_date)

ggplot(tourn_stats) + 
  geom_point(size = 3, mapping=aes(x=match_date, y=Shots, color = team)) +
  xlab("Match Date")+
  ylab("Number of Shots")+
  geom_line(aes(x = match_date, y = Shots, group = team))

ggplot(tourn_stats, aes(x=match_date, y=Shots, color = team, group = team)) + 
  geom_point(size = 3) +
  xlab("Match Date")+
  ylab("Number of Shots")+
  geom_line()
1 Like

Try to remember like this - everything that affects the plot itself goes into r aes() function, including in the r geom_point() part

1 Like

It seems like you're encountering issues with the x-axis and date formatting in your line graph. To resolve this, ensure that the 'match_date' column is correctly recognized as the x-axis, and make sure your date format is consistent.

Double-check your code for any potential errors or typos in column names and date formatting functions.

Good luck with your graph!

1 Like

I appreciate the help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.