Reordering/Renaming X-Axis Labels

I am part of a Mario Kart league. I'm trying to create a visualization to compare the length of time each person was champion. I have a CSV with the following columns: name, date_won, days_as_champ.

Here is the code I initially tried:

ggplot(data=champs, aes(x=date_won, y=days_as_champ, group=1))+
  geom_line()+
  geom_point()

This produces the correct line, but the x-axis labels are (obviously) the dates, which aren't very useful.

I tried using 'name' for the x-axis, but that brings up two issues. First, the names are in alphabetical order instead of the order they are in the data frame. Secondly, instead of having a separate entry for each time the individual won, it places all points above the single instance of the name.

Which would be the better approach, using 'date_won' as the axis and remapping the labels to 'name' or using 'name' for the x-axis but reordering them based on the 'date_won' field? And how would I go about doing it?

I thought this would be something simple enough to do, but no amount of searching or documentation reading has produced any solutions.

Can you post some or all of your data? A simple way to do that is to post the output of

dput(champs)

If that is too much output, post the output of something like

dput(head(champs, 25))

where 25 is the number of rows of data. Tune that number to something reasonable for your data.

Sure, it's a small data set that I'm just using to learn R.

structure(list(name = c("Jason", "Amanda", "Jason", "Dave", "Briona", 
"Sam", "Jason", "Sam", "Kyle", "Sam", "Kyle", "Kayleigh", "Adri", 
"Dave", "Adri", "Marlon", "Ali", "Marlon", "LJ", "Chris"), date_won = c("23-May-20", 
"13-Aug-20", "7-Oct-20", "7-Nov-20", "12-May-21", "29-Sep-21", 
"22-Dec-21", "26-Jan-22", "9-Mar-22", "15-Apr-22", "27-Apr-22", 
"6-Jul-22", "19-Nov-22", "4-Jan-23", "1-Feb-23", "5-Mar-23", 
"8-Mar-23", "19-Apr-23", "17-Jun-23", "23-Sep-23"), days_as_champ = c(82L, 
55L, 31L, 186L, 140L, 84L, 35L, 42L, 37L, 12L, 70L, 136L, 46L, 
28L, 32L, 3L, 42L, 59L, 98L, 32L)), class = "data.frame", row.names = c(NA, 
-20L))

I think the following code gives a plot close to what you want. Your original ggplot code produces a plot with the dates as characters (they are within quotes in the data frame) so they are arranged alphabetically. I chose to get around that by making a new column that is simply the row number and using that as the x variable. I then used the name column to label the x axis. There are other ways to get to the same result. My code changes the date_won column to a numeric date and then sorts on that column. That is to ensure that the rows are in date-order, in case that isn't always true in the raw data.

  library(ggplot2)
  library(dplyr)
  library(lubridate)
  champs |> mutate(date_won = dmy(date_won)) |> 
    arrange(date_won) |> 
    mutate(dummy = row_number()) |>   
    ggplot(aes(x=dummy, y=days_as_champ, group=1))+
    geom_line()+
    geom_point()+ labs(x = "name") +
    scale_x_continuous(breaks = 1:20, labels = champs$name) +
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5))

Thank you very much, that's exactly what I was trying to do! Glad to see I wasn't completely off in my attempts, I had messed with scale_x_continuous and had tried l mutating the list in various ways but hadn't found the correct combination of elements to pull it all together.

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.