Indescribled
Indescribled

Reputation: 394

Heatmap via geom_tile ggplot R - Organize y axis levels of monthly factor correctly

I am trying to make a heatmap of dates. I converted it to an example you can copy/paste to R to see.

The first attempt at a plot works correctly EXCEPT the months on the x axis are not in order. I attempt to order them by adding levels. In the next plot, the order is correct, but the data did not move. The same data shows in Feb 2009 and Aug 2009. Aug 2009 is correct, but when I tried to fix the levels the data did not move. How can I have the X axis labelled in order and have the data be correct at the same time?

library(tidyverse)

year_data <- c("2009", rep("2010",7), rep("2011",10),rep("2012",10))

month_data <- c("Aug", "Aug", "Feb", "Jan", "Jul", "May", "Nov", "Oct", "Aug",
                "Dec", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep",
                "Apr", "Aug", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Oct", "Sep")

number_data <- c(3, 12, 6, 3, 15, 6, 9, 6, 30, 24, 3, 24, 12, 12, 6, 39, 33, 39, 
                 33, 51, 45, 54, 42, 30, 36, 45, 15, 36)

reprex_data <- data.frame(year_data, month_data, number_data) %>% 
  as_tibble() %>% 
  rename("year" = 1,
         "month" = 2,
         "n" = 3) %>% 
  mutate(month = as.factor(month))

# This plot works, but y axis is out of order

reprex_data %>%
  ggplot(aes(year, month)) +
  geom_tile(aes(fill = n)) +
  scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black")) +
  labs(title = "Plot before attempting to fix levels")

enter image description here

# Attempt to reorganize them. While it works, the data that should be Aug, 2009 is plotted as Feb, 2009
levels(reprex_data$month) <- (month.abb)

reprex_data %>%
  ggplot(aes(year, month)) +
  geom_tile(aes(fill = n)) +
  scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black")) +
  labs(title = "Plot after attempting to fix levels")

enter image description here

Upvotes: 2

Views: 842

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388907

You can try -

library(tidyverse)

reprex_data <- data.frame(year_data, month_data, number_data) %>% 
  as_tibble() %>% 
  rename("year" = 1,
         "month" = 2,
         "n" = 3) %>%
  mutate(month = factor(month, month.abb))

and then use the plot code -

reprex_data %>%
  ggplot(aes(year, month)) +
  geom_tile(aes(fill = n)) +
  scale_fill_gradient(low = "#d8e1cf", high = "#438484") +
  theme_bw() +
  theme(panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_line(colour = "black")) +
  labs(title = "Fixed Plot")

enter image description here

Upvotes: 1

Related Questions