1

A minimum working example code:

df <- data.frame()
df[1:2,"year"] <- 2012L
df[3:7, "year"] <- 2013L
df[8:16, "year"] <- 2014L
df$team <- c("Team_A", "Team_B", "Team_A", "Team_B", "Team_C",
         "Team_D", "Team_E", "Team_A", "Team_B", "Team_C",
         "Team_D", "Team_E", "Team_F", "Team_G", "Team_H",
         "Team_I")
df$matches <- c(4, 2, 9, 7, 3, 4, 1, 16, 13, 10, 8, 9, 2,3,1, 3)
df

The dataframe looks like this:

    year     team  matches
1   2012    Team_A  4
2   2012    Team_B  2
3   2013    Team_A  9
4   2013    Team_B  7
5   2013    Team_C  3
6   2013    Team_D  4
7   2013    Team_E  1
8   2014    Team_A  16
9   2014    Team_B  13
10  2014    Team_C  10
11  2014    Team_D  8
12  2014    Team_E  9
13  2014    Team_F  2
14  2014    Team_G  3
15  2014    Team_H  1
16  2014    Team_I  3

The matches are plot yearwise:

match_plot_yearwise <- ggplot(df, aes(matches, team, color = year)) +
  geom_segment(aes(x = matches, y = team, xend = matches, yend = team), 
                   color= "grey50") +
  geom_point() +
  theme_minimal() +
  theme(
    panel.grid = element_blank(),
  ) 
match_plot_yearwise

This gives the following plot: enter image description here

If a team plays more than one year then, how to repeat the team name on y-axis that many times, instead of stacking it's each year match one above the other?

4
  • 1
    Thanks for your question. Unfortunately it is difficult to determine what you need from this information alone. Can you please provide an example of code that you have tried so far, and explain more clearly why it doesn't give the desired result? Commented Feb 20, 2020 at 13:36
  • Provided an example of code Commented Feb 20, 2020 at 14:15
  • maybe you would like to consider creating subplots to make different plots for each year. Also try to keep year on x- axis, or make year a factor instead of continuous variable. Commented Feb 20, 2020 at 14:18
  • Thanks for your response.. However, I'm afraid, I don't want to use subplots.. Commented Feb 20, 2020 at 14:21

1 Answer 1

1

This solution pastes team and year together in a single variable, but labels the y-axis with the team name only. I could not tell what you wanted to do with the geom_segment() so I omitted it. (In your example plots it creates segments with length 0.) I had to sort df so that the labels would appear in the correct places.

data

df <- read.table(textConnection('    year     team  matches
1   2012    Team_A  4
2   2012    Team_B  2
3   2013    Team_A  9
4   2013    Team_B  7
5   2013    Team_C  3
6   2013    Team_D  4
7   2013    Team_E  1
8   2014    Team_A  16
9   2014    Team_B  13
10  2014    Team_C  10
11  2014    Team_D  8
12  2014    Team_E  9
13  2014    Team_F  2
14  2014    Team_G  3
15  2014    Team_H  1
16  2014    Team_I  3'))

code

library(dplyr)
library(ggplot2)

df <- df %>% 
  arrange(team, year)

ggplot(df, aes(x = matches, y = interaction(year, team), color = factor(year))) +

  geom_point() +
  scale_y_discrete(labels = df$team) +
  theme_minimal() +

  theme(
    panel.grid = element_blank(),
  ) 

output

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

That helps! Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.