Magnus
Magnus

Reputation: 760

How to I replace specific values with text in ggplot2?

I'm trying to visualize the language skills of our students per program and over time. That would look something like this:

enter image description here

This is however not totally intuitive, so I would like to replace the y axis values so that:

> 20.0 codes for A, 
> 17.5 codes for B
> 15.0 codes for C
> 12.5 codes for D
> 10.0 codes for E.

I try to augment my original code and I write the following:

totdata%>%filter(program=="Ekonom")%>%
ggplot(aes(x=ADMISSIONROUND_ID,y=SVENSKA))+
geom_boxplot()**+
scale_y_continuous(labels=c("20.0" = "A","17.5" = "B", "15.0" = "C", "12.5"="D", "10"="E"))**

This produces the following graph, which is the opposite of what I want. The values has been replaced in the opposite order, so that the highest values is E rather than A.

enter image description here

Is there any way for me to preserve the original figure but replace my numbers with text values, as described above?

Small sample of data:

structure(list(start_date = structure(c(15585, 15585, 15585, 
15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 
15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 
15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 
15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 
15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 15585, 
15585, 15585), class = "Date"), SVENSKA = c(15, 15, 15, 10, 15, 
15, 15, 10, 15, 20, 20, 15, 15, 20, 20, 20, 15, 20, 15, 10, 15, 
20, 10, 20, 15, 15, 10, 15, 10, 20, 10, 15, 15, NA, 15, 10, NA, 
20, 20, 20, 20, 20, 15, 15, 20, 15, 15, 15, 15, 20), ADMISSIONROUND_ID = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("HT2012", "HT2013", "HT2014", "HT2015", "HT2016", 
"HT2017", "HT2018", "HT2019"), class = c("ordered", "factor")), 
    program = c("Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", "Ekonom", 
    "Ekonom", "Ekonom", "Ekonom")), row.names = c(NA, -50L), groups = structure(list(
    start_date = structure(15585, class = "Date"), .rows = list(
        1:50)), row.names = c(NA, -1L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

Upvotes: 1

Views: 1067

Answers (1)

lroha
lroha

Reputation: 34291

You can keep things dymanic by passing a function to the labels argument of scale_y_continous(), in this case reverse LETTERS of the length of the breaks:

df %>% 
  filter(program == "Ekonom") %>%
  ggplot(aes(x = ADMISSIONROUND_ID, y = SVENSKA)) +
  geom_boxplot() +
  scale_y_continuous(labels = function(x) rev(LETTERS[seq_along(x)]))

enter image description here

Upvotes: 1

Related Questions