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:
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