3

Using ggplot2, I want to create a plot with a variable as my y-axis, but a different variable for the y-axis label

Take this sample data:

df <- data_frame(
  YearMonth = c("2018-01", "2018-01", "2018-02", "2018-02", "2018-03", "2018-03", "2018-04", "2018-04", "2018-05", "2018-05", "2018-06", "2018-06", "2019-01", "2019-01", "2019-02", "2019-02", "2019-03", "2019-03", "2019-04", "2019-04", "2019-05", "2019-05", "2019-06", "2019-06"),
  Year = c("2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019"),
  Day = c("1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"),
  Total = c(11, 43, 12, 81, 90, 74, 8, 3, 7, 14, 50, 99, 67, 45, 31, 23, 46, 53, 4, 90, 34, 46, 98, 2),
)

I can create a tile plot, with "year-month" as my y-axis, "Day" as my x-axis, & "Total" my "fill" value

library(ggplot2)

df %>% 
  ggplot(aes(x = Day,
             y = YearMonth)) + 
  geom_tile(aes(fill = Total),
            colour = "white",
            size = 0.2,
            na.rm = FALSE) + 
  coord_equal(ratio = 1) + 
  scale_fill_gradient2(low = "#F1EDDE", 
                       high = "#C72B26", 
                       midpoint = 10) 

which produces the below:

enter image description here

However, I don't want "Year-Month" as my y-axis label. I want to use "Year" - which would produce just two labels (2018 & 2019) along the left hand, vertical axis

I have consulted similar topics with no such success. I have attempted to use annotate, specified "year" in scale_y_discrete, & a secondary axis, but have not been able to achieve the desired result. I would be grateful for any instruction on how to achieve this

perkot
  • 121
  • 1
  • 10
  • Do you want only 4 tiles or do you want the plot as it is and change only the y tick labels? – Roman Dec 09 '20 at 10:36

2 Answers2

1

You can feed explicit breaks and labels to the scale, or provide a function that gets you the result you want. One example of each in the code below. I've assumed you wanted to label the januaries.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.0.3
#> Warning: package 'tidyr' was built under R version 4.0.3
#> Warning: package 'dplyr' was built under R version 4.0.3

df <- data.frame(
  YearMonth = c("2018-01", "2018-01", "2018-02", "2018-02", "2018-03", "2018-03", "2018-04", "2018-04", "2018-05", "2018-05", "2018-06", "2018-06", "2019-01", "2019-01", "2019-02", "2019-02", "2019-03", "2019-03", "2019-04", "2019-04", "2019-05", "2019-05", "2019-06", "2019-06"),
  Year = c("2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2018", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019", "2019"),
  Day = c("1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2","1","2"),
  Total = c(11, 43, 12, 81, 90, 74, 8, 3, 7, 14, 50, 99, 67, 45, 31, 23, 46, 53, 4, 90, 34, 46, 98, 2)
)

df %>% 
  ggplot(aes(x = Day,
             y = YearMonth)) + 
  geom_tile(aes(fill = Total),
            colour = "white",
            size = 0.2,
            na.rm = FALSE) + 
  coord_equal(ratio = 1) + 
  scale_fill_gradient2(low = "#F1EDDE", 
                       high = "#C72B26", 
                       midpoint = 10) +
  scale_y_discrete(
    breaks = c("2018-01", "2019-01"),
    labels = c("2018", "2019")
  )


df %>% 
  ggplot(aes(x = Day,
             y = YearMonth)) + 
  geom_tile(aes(fill = Total),
            colour = "white",
            size = 0.2,
            na.rm = FALSE) + 
  coord_equal(ratio = 1) + 
  scale_fill_gradient2(low = "#F1EDDE", 
                       high = "#C72B26", 
                       midpoint = 10) +
  scale_y_discrete(
    breaks = function(x) { x[grepl("-01$", x)] },
    labels = function(x) {data.table::tstrsplit(x, "-")[[1]]}
  )

Created on 2020-12-09 by the reprex package (v0.3.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
1

You can try

df %>% 
  mutate(YearMonth = str_split(YearMonth, "-") %>% map_chr(2) %>% as.numeric) %>%
  mutate(Year = factor(Year, levels = rev(unique(as.numeric(Year))))) %>% 
  ggplot(aes(x = Day,
             y = YearMonth)) + 
  geom_tile(aes(fill = Total),
            colour = "white",
            size = 0.2,
            na.rm = FALSE) + 
  scale_fill_gradient2(low = "#F1EDDE", 
                       high = "#C72B26", 
                       midpoint = 10) +
  coord_equal() +
  facet_wrap(~Year,ncol=1, strip.position = "left") +
  ylab("") +
  theme_bw(base_size = 16) + 
  theme(axis.text.y = element_blank(), 
        axis.ticks.y = element_blank())

enter image description here

Roman
  • 17,008
  • 3
  • 36
  • 49
  • this is a nice way of visualising it - teunbrand had the answer I was looking for, but thank you for responding – perkot Dec 09 '20 at 10:46