Im trying to make a heatmap, that illustrates how intense a newspaper write about 30 different news topics over a timeperiod.
The news topics can be grouped into 6 "meta topics" illustrated by the 6 different colors in the diagram below. However, I would like to fill out the color in each box of the upper legend, so it is easier to see (right now the color is only encircling each of the categories in the legend.)
Secondly, I would like to change the location of the upper legend such that it is above the diagram. I have tried to add "theme(legend.position="top") + " to the code, but it doesn't change anything.
My code is:
all.data %>%
dplyr::mutate(TopicName = fct_reorder(TopicName, metanumber)) %>%
ggplot(aes(x = date, y = TopicName, 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)) +
theme(legend.position="bottom") +
scale_colour_brewer(palette = "Dark2", name=NULL) +
scale_fill_gradient(low = "white",high = "black", name=NULL) +
labs(x=NULL, y="News topic") +
theme_light(base_size = 11)
Update: To reproduce the structure of the data, see the following code:
structure(list(SeriesID = c("Topic_1", "Topic_2", "Topic_1", "Topic_2", "Topic_1", "Topic_2"), date = structure(c(14760, 14760, 14790, 14790, 14821, 14821), class = "Date"), TopicName = c("Sport","Soccer", "Sport", "Soccer", "Sport", "Soccer"),MetaTopic = c("Sport", "Sport", "Sport", "Sport", "Sport", "Sport"),abs_impact = c(0.00169196071242834, 0.00237226605899713, 0.00031583852881164, 0.00096867233821691, 0.00020904777100742, 0.00023139444960141), sum = c(0.196227808854163, 0.196227808854163,0.047504294243804, 0.047504294243804,0.0296850112874241, 0.0296850112874241),rel_impact = c(0.00862243084865617,0.01208934693227, 0.00664863111512987,0.0203912583827778, 0.00704219947849513, 0.00779499281172378), metanumber = c(1, 1, 1, 1, 1, 1)), row.names= c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
Hope you can help me out.