Search code examples
rggplot2colorsheatmapgeom-tile

Change color filling of heatmap (ggplot)


Im trying to making a heatmap that illustrates how intense a newspaper writes about 6 different meta topics over a time period. I would like each topic to have its own color. The more intense the color is, the more the newspaper writes about it relative to the other meta topics (corresponds to the rel_impact variable in the data set). So far I have only been able to change the circumference of each box, so the topics get different colors. See the figure below. However, I would like that each box is completly filled out in line with the color scale indicated with the "meta topic" legend instead of the grey color scale.

enter image description here

My code is:

my_palette <- RColorBrewer::brewer.pal(6, 'Dark2')

metatopic_data %>%
  ggplot(aes(x = date, y = Metatopic, color=Metatopic,fill = rel_impact)) + 
geom_tile() +
  scale_x_date(date_breaks = "1 year", date_labels = "%Y",expand = c(0,0)) +
  scale_y_discrete(expand=c(0,0)) +
  scale_colour_brewer(palette = "Dark2", name="Meta topics") +
  scale_fill_gradient(low = "white",high = "black", name=NULL) +
  guides(color = guide_legend(override.aes = list(fill = my_palette))) +
  theme_light(base_size = 11) +
  labs(x=NULL, y=NULL)

To reproduce the structure of the data, see the following code:

structure(list(date = structure(c(14760, 14760, 14760, 14760, 
14760, 14760), class = "Date"), Metatopic = c("Career", "Economics", 
"Industries", "Leisure", "Politics", "Sport"), abs_impact = c(0.00531062385448913, 
0.0569595367458113, 0.0459819861634464, 0.00889034813748066, 
0.0750210871815098, 0.00406422677142547), sum = c(0.196227808854163, 
0.196227808854163, 0.196227808854163, 0.196227808854163, 0.196227808854163, 
0.196227808854163), rel_impact = c(0.0270635639540571, 0.290272500510587, 
0.234329611240884, 0.0453062600525087, 0.382316286461037, 0.0207117777809261
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(date = structure(14760, class = "Date"), 
    .rows = structure(list(1:6), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))

Do anyone have a suggestions how I can do this?


Solution

  • You could use the same palette for colour and fill, and adjust opacity (alpha) via the rel_impact. The following code omits tile borders, which - with tiles that narrow - would mostly override the impression of opacity:

    metatopic_data %>%
      ggplot(aes(x = date, y = Metatopic, fill = Metatopic, alpha = rel_impact), color = NA) + 
      geom_tile() +
      scale_x_date(date_breaks = "1 year", date_labels = "%Y",expand = c(0,0)) +
      scale_y_discrete(expand=c(0,0)) +
      scale_fill_brewer(palette = "Dark2", name="Meta topics") +
      guides(color = guide_legend(override.aes = list(fill = my_palette))) +
      theme_light(base_size = 11) +
      labs(x=NULL, y=NULL)
    

    heatmap with opacity