I have world record data and wanting to create a plot. Y axis is time of records in seconds so I want to relabel with m:ss. When I use the following code the entire axis labels just disappear? (see image, just removes y labels completely)
ggplot(men, aes(x=Year, y=Time, colour=Nationality)) +
geom_point()+
scale_y_discrete(labels = c('210' = '3m30s', '240' = '4m00s', '270' = '4m30s'))
You will have to fiddle a bit with the breaks and labels as well as the limits, but this is one way to do it.
You define the breaks and the labels separately and then define those in the scale_y_continous. This of course requires you to ensure that your times are numeric.
library(tidyverse)
library(lubridate)
dat <- structure(list(Time = c("3:55.8", "3:54.7", "3:52.6", "3:51.0",
"3:49.2", "3:49.2", "3:49.0", "3:48.8", "3:47.8", "3:47.6", "3:45.8",
"3:45.0", "3:43.0", "3:43.0", "3:43.0", "3:42.8+", "3:41.8+",
"3:40.8", "3:40.8", "3:40.8", "3:40.6", "3:40.2", "3:40.2", "3:38.1",
"3:36.0", "3:35.6", "3:33.1", "3:32.2", "3:32.1", "3:32.1", "3:31.4",
"3:31.24", "3:30.77", "3:29.67", "3:29.46", "3:28.86", "3:27.37",
"3:26.00"), Athlete = c(" Abel Kiviat (USA)", " John Zander (SWE)",
" Paavo Nurmi (FIN)", " Otto Peltzer (GER)", " Jules Ladoumegue (FRA)",
" Luigi Beccali (ITA)", " Luigi Beccali (ITA)", " Bill Bonthron (USA)",
" Jack Lovelock (NZL)", " Gunder Hägg (SWE)", " Gunder Hägg (SWE)",
" Arne Andersson (SWE)", " Gunder Hägg (SWE)", " Lennart Strand (SWE)",
" Werner Lueg (FRG)", " Wes Santee (USA)", " John Landy (AUS)",
" Sándor Iharos (HUN)", " László Tábori (HUN)", " Gunnar Nielsen (DEN)",
" István Rózsavölgyi (HUN)", " Olavi Salsola (FIN)", " Olavi Salonen (FIN)",
" Stanislav Jungwirth (TCH)", " Herb Elliott (AUS)", " Herb Elliott (AUS)",
" Jim Ryun (USA)", " Filbert Bayi (TAN)", " Sebastian Coe (GBR)",
" Steve Ovett (GBR)", " Steve Ovett (GBR)", " Sydney Maree (USA)",
" Steve Ovett (GBR)", " Steve Cram (GBR)", " Saïd Aouita (MAR)",
" Noureddine Morceli (ALG)", " Noureddine Morceli (ALG)",
" Hicham El Guerrouj (MAR)"), Date = c("08-06-1912", "05-08-1917",
"19-06-1924", "11-09-1926", "05-10-1930", "09-09-1933", "17-09-1933",
"30-06-1934", "06-08-1936", "10-08-1941", "17-07-1942", "17-08-1943",
"07-07-1944", "15-07-1947", "29-06-1952", "04-06-1954", "21-06-1954",
"28-07-1955", "06-09-1955", "06-09-1955", "03-08-1956", "11-07-1957",
"11-07-1957", "12-07-1957", "28-08-1958", "06-09-1960", "08-07-1967",
"02-02-1974", "15-08-1979", "15-07-1980", "27-08-1980", "28-08-1983",
"04-09-1983", "16-07-1985", "23-08-1985", "06-09-1992", "12-07-1995",
"14-07-1998")), class = "data.frame", row.names = c(NA, -38L))
new_dat <- dat |>
separate(Athlete, into = c("Athelete", "Nationality"), sep = "\\(") |>
mutate(Nationality = str_remove(Nationality, "\\)"), #Cleaning up the data copied from Wiki
Time = str_remove(Time, "\\+"), #Cleaning up the data copied from Wiki
Time = period_to_seconds(ms(Time)), #Converting to seconds
Date = dmy(Date))
breaks <- seq(200, 240, length.out = 5) # Breaks and labels need to be of even length!
labels <- c("03:20", "03:30", "03:40", "03:50", "04:00")
new_dat |>
ggplot(aes(x = Date, y = Time)) +
geom_point(aes(col = Nationality)) +
scale_y_continuous(limits = c(200, 240),
breaks = breaks,
labels = labels)
Created on 2023-03-03 with reprex v2.0.2