Search code examples
rggplot2levels

R ggplot2 Visualize categorical variable that levels appear more than once


I am trying to visualize some tennis data with ggplot2 in R.

Here are my data:

     Year<-c(1999:2020)
     Player <- rep("Federer",22)
     Rank <- 
     c("Q1","3R","3R","4R","4R","W","SF","W","W","SF","F","W","SF","SF","SF","SF","3R",
               "SF","W","W","4R","SF")
     data <- data.frame(Year, Player, Rank)
     data$Rank <- factor(data$Rank, levels = unique(data$Rank))

What I want to do is a diagram that looks like a bar plot but actually is not a bar plot. I would like to have as x-axis Years from 1999 to 2020 and correspond them to Rank level.

My problem is that Rank, which is I converted to categorical variable, has some levels that appear more than once in time and this makes things difficult for me.

I am looking to do something like the following pic from Wikipedia with specific color for every level of Rank variable.

enter image description here

The Australian open result is what I want to visualize.


Solution

  • Maybe something like this, using geom_tile() to make like a heatmap..instead of a barplot:

    library(ggthemes)
    ggplot(data,aes(x=factor(Year),y=Player,fill=Rank)) + 
    geom_tile() + scale_fill_economist()
    

    enter image description here