修改热力图的填充颜色(ggplot)。

huangapple go评论71阅读模式
英文:

Change color filling of heatmap (ggplot)

问题

我试图创建一个热力图,用于说明报纸在一段时间内如何强烈地报道了6个不同的元主题。我希望每个主题都有自己的颜色。颜色越浓,报纸对它的报道相对于其他元主题越多(对应于数据集中的rel_impact变量)。
到目前为止,我只能改变每个框的轮廓,以便主题获得不同的颜色。请参见下面的图。但是,我希望每个框都完全填充,符合“元主题”图例中指示的颜色标度,而不是灰色标度。

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)

要复制数据的结构,请参见以下代码:

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

是否有人有建议,我该如何做到这一点?

英文:

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.

修改热力图的填充颜色(ggplot)。

My code is:

my_palette &lt;- RColorBrewer::brewer.pal(6, &#39;Dark2&#39;)

metatopic_data %&gt;%
  ggplot(aes(x = date, y = Metatopic, color=Metatopic,fill = rel_impact)) + 
geom_tile() +
  scale_x_date(date_breaks = &quot;1 year&quot;, date_labels = &quot;%Y&quot;,expand = c(0,0)) +
  scale_y_discrete(expand=c(0,0)) +
  scale_colour_brewer(palette = &quot;Dark2&quot;, name=&quot;Meta topics&quot;) +
  scale_fill_gradient(low = &quot;white&quot;,high = &quot;black&quot;, 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 = &quot;Date&quot;), Metatopic = c(&quot;Career&quot;, &quot;Economics&quot;, 
&quot;Industries&quot;, &quot;Leisure&quot;, &quot;Politics&quot;, &quot;Sport&quot;), 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(&quot;grouped_df&quot;, &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, 
-6L), groups = structure(list(date = structure(14760, class = &quot;Date&quot;), 
    .rows = structure(list(1:6), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
    &quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
), row.names = c(NA, -1L), .drop = TRUE))

Do anyone have a suggestions how I can do this?

答案1

得分: 1

你可以在颜色和填充方面使用相同的调色板,并通过rel_impact来调整不透明度 (alpha)。以下代码省略了瓷砖边框,因为对于这么窄的瓷砖,边框会大部分覆盖不透明度的印象:

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)

修改热力图的填充颜色(ggplot)。

英文:

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 %&gt;%
  ggplot(aes(x = date, y = Metatopic, fill = Metatopic, alpha = rel_impact), color = NA) + 
  geom_tile() +
  scale_x_date(date_breaks = &quot;1 year&quot;, date_labels = &quot;%Y&quot;,expand = c(0,0)) +
  scale_y_discrete(expand=c(0,0)) +
  scale_fill_brewer(palette = &quot;Dark2&quot;, name=&quot;Meta topics&quot;) +
  guides(color = guide_legend(override.aes = list(fill = my_palette))) +
  theme_light(base_size = 11) +
  labs(x=NULL, y=NULL)

修改热力图的填充颜色(ggplot)。

huangapple
  • 本文由 发表于 2023年7月12日 21:30:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定