Reputation: 520
This input data is from dput:
structure(list(Player = c("deGrom", "deGrom", "deGrom", "deGrom",
"deGrom", "deGrom", "deGrom", "Wheeler", "Wheeler", "Wheeler",
"Wheeler", "Wheeler", "Wheeler", "Syndergaard", "Syndergaard",
"Syndergaard", "Syndergaard", "Matz", "Matz", "Matz", "Matz",
"Matz", "Stroman", "Stroman"), GSc = c(66, 70, 77, 77, 79, 78,
79, 76, 70, 64, 70, 62, 70, 69, 73, 81, 62, 68, 62, 69, 68, 70,
63, 75)), row.names = c(NA, -24L), class = c("tbl_df", "tbl",
"data.frame"))
I have a data frame MetsGS3 with the data above.
I want to use ggplot to create a line chart with a different color line for each of the five players. The x-axis will contain the numbers 2, 4, 6, 8, 10, 12. The y-axis will contain the game scores (GS2). I want the x-axis label to be Player and the y-axis label to be Game Score.
This code does not work, and I need help getting it to work. I know it is missing elements.
ggplot(MetsGS, aes(x=MetsGS$Player, y=GSc, colour = MetsGS$Player) + geom_line(size=1.2) + ggtitle("Mets Game Score Game Scores")
The last time I ran the above ggplot code in RStudio I got this error: "Error: Incomplete expression: ggplot(MetsGS, aes(x=MetsGS$Player, y=GSc, colour = MetsGS$Player) + geom_line(size=1.2) + ggtitle("Mets Game Score Game Scores")"
Thanks in advance, Howard
Upvotes: 0
Views: 379
Reputation: 16178
I think there is some data missing in your dataset. I can't find how you are defining x
as a number comprised between 2 and 12.
So, I assumed that for each player, each line containing the name of the player correspond to a different game. So, I create a new column using dplyr
as this (I called your dataframe d
):
library(dplyr)
d %>% group_by(Player) %>% mutate(Number = seq_along(Player)*2)
# A tibble: 24 x 3
# Groups: Player [5]
Player GSc Number
<chr> <dbl> <dbl>
1 deGrom 66 2
2 deGrom 70 4
3 deGrom 77 6
4 deGrom 77 8
5 deGrom 79 10
6 deGrom 78 12
7 deGrom 79 14
8 Wheeler 76 2
9 Wheeler 70 4
10 Wheeler 64 6
# … with 14 more rows
and plot it like this:
library(ggplot2)
library(dplyr)
d %>% group_by(Player) %>% mutate(Number = seq_along(Player)*2) %>%
ggplot(., aes(x=Number, y=GSc, colour = Player)) +
geom_line(size=1.2) +
ggtitle("Mets Game Score Game Scores")+
scale_x_continuous(breaks = seq(2,14, by = 2))
Does it look what you are looking for ? If not, can you clarify your question
Upvotes: 2