R语言 更改热图中图例的颜色填充和位置(gglot)

brccelvz  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(495)

我想做一个热图,说明一份报纸在一段时间内写30个不同的新闻主题是多么激烈。新闻主题可以分为6个“ meta主题”,如下图中的6种不同颜色所示。但是,我想在上面图例的每个框中填写颜色,这样更容易看到(现在颜色只包围图例中的每个类别。)其次,我想改变上部图例的位置,使其位于图表上方。我已经尝试添加“主题(传奇。position=“top”)+“添加到代码中,但它不会改变任何内容。

我的代码是:

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)

更新:要重现数据的结构,请参阅以下代码:

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"))

希望你能帮我。

laawzig2

laawzig21#

编辑:单独移动图例

我随意更改了MetaTopic变量中的一些标签,以使对图的更改更加明显。
您可以覆盖color图例中使用的fill外观,而不会影响其他图例和贴图。我制作了一个调色板来匹配您的色标,在示例中命名为my_palette。然后我添加guide(color = guide_legend(override.aes = list(fill = my_palette)))到您的图中,以填充颜色图例。
对于您的图,您需要更改调色板中的颜色数量以匹配元主题的数量(在我的示例中为3)。还要注意,我增加了geom_tileslinewidth,只是为了在本例中更容易看到。您可以将其删除以恢复为默认值。
独立移动图例需要多几个步骤。首先,我们创建图,图例位于右侧的默认位置。接下来,使用cowplot::get_legend隔离图例,并删除color美学的图例(将其添加到其他地方)。最后,我们将图分为两部分:将fill覆盖和位置更改添加到没有填充图例的图中,并添加回我们在上一步中提取的填充图例。rel_widths设置每列的相对宽度,在本例中填充图例为第2列。

library(tidyverse)
library(cowplot)

all.data <- 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", "Leisure", "Sport", "Career", "Career", "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"))

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

# base plot
p1 <- all.data %>%
  dplyr::mutate(TopicName = fct_reorder(TopicName, metanumber)) %>%
  ggplot(aes(x = date, y = TopicName, color=MetaTopic,fill = rel_impact)) + 
  geom_tile(linewidth = 1) +
  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)

# grab fill legend
guide_color <- get_legend(p1 + guides(color = 'none'))

# construct plot
plot_grid(p1 +
            guides(fill = 'none',
                   color = guide_legend(
                     override.aes = list(fill = my_palette))) +
            theme(legend.position = 'top'),
          guide_color,
          ncol = 2,
          rel_widths = c(0.9, 0.1)
          )

创建于2023-05-02使用reprex v2.0.2

相关问题