修改ggplot2中分组条形图中特定条形的颜色。

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

R - Change colors of specific bars in a dodged barplot in ggplot2

问题

我想改变分组条形图中特定日期的颜色。

复制现有图表的代码如下:

df <- data.frame(mat = c("Apr-24", "Nov-24", "Apr-25", "Nov-25", "Apr-26", "Sep-26", "Apr-27"),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 <- gather(df, event, total, term_rv:zc_rv)

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = "identity", position = 'dodge') +
  theme_bw() +
  scale_fill_manual(name = c("Method"), 
                    label = c("TF", "ZC"), 
                    values = c("#ff8080", "#00cccc"))

这应该会生成一个类似于这样的图表:

我想要与Nov-24关联的两个条形图的颜色不同(或具有边框),以便与其他条形图区分开。所以类似于这样的效果:

是否有一种简单的方法可以做到这一点?请随意更改代码。我使用gather来创建分组图表,但可能有更简单的方法。

英文:

I'd like to change the colour of a specific date in a dodged bar chart.

The code to replicate the existing chart is as follows:

df &lt;- data.frame(mat = c(&quot;Apr-24&quot;, &quot;Nov-24&quot;, &quot;Apr-25&quot;, &quot;Nov-25&quot;, &quot;Apr-26&quot;, &quot;Sep-26&quot;, &quot;Apr-27&quot;),
             term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
             zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 &lt;- gather(df, event, total, term_rv:zc_rv)

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                values = c(&quot;#ff8080&quot;, &quot;#00cccc&quot;))

This should produce a chart like this:
修改ggplot2中分组条形图中特定条形的颜色。

I'd like the two bars associated with Nov-24 to be in a different colour (or with borders) to differentiate them with the rest. So something like this:

修改ggplot2中分组条形图中特定条形的颜色。

Is there a simple way to do this? Feel free to change the code in anyway. I used gather to create the dodged chart but there might be a easier way.

答案1

得分: 2

I typically use the gghighlight package for this type of task, e.g.

library(tidyverse)
library(gghighlight)

df &lt;- data.frame(mat = c(&quot;Apr-24&quot;, &quot;Nov-24&quot;, &quot;Apr-25&quot;, &quot;Nov-25&quot;, &quot;Apr-26&quot;, &quot;Sep-26&quot;, &quot;Apr-27&quot;),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 &lt;- df %&gt;% 
  pivot_longer(cols = -mat,
               names_to = &quot;event&quot;,
               values_to = &quot;total&quot;)

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                    label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                    values = c(&quot;#ff8080&quot;, &quot;#00cccc&quot;)) +
  gghighlight(mat == &quot;Nov-24&quot;)

修改ggplot2中分组条形图中特定条形的颜色。


If you use the 'darker colours' for all of the bars, then replace the unhighlighted bars with your 'regular colours', you can get your desired outcome:

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                    label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                    values = c(&quot;#870017&quot;, &quot;#3E4EC8&quot;)) +
  gghighlight(mat == &quot;Nov-24&quot;,
              unhighlighted_params = list(fill = rep(c(&quot;#ff8080&quot;, &quot;#00cccc&quot;),
                                                     length(unique(df2$mat)))))

修改ggplot2中分组条形图中特定条形的颜色。

Created on 2023-06-29 with reprex v2.0.2


A potential downside to this approach is the legend is lost. One potential workaround for this is to label the bars of interest, e.g.

library(tidyverse)
library(gghighlight)
library(ggrepel)

df &lt;- data.frame(mat = c(&quot;Apr-24&quot;, &quot;Nov-24&quot;, &quot;Apr-25&quot;, &quot;Nov-25&quot;, &quot;Apr-26&quot;, &quot;Sep-26&quot;, &quot;Apr-27&quot;),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 &lt;- df %&gt;% 
  pivot_longer(cols = -mat,
               names_to = &quot;event&quot;,
               values_to = &quot;total&quot;)

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                    label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                    values = c(&quot;#ff8080&quot;, &quot;#00cccc&quot;)) +
  gghighlight(mat == &quot;Nov-24&quot;) +
  geom_text_repel(aes(label = event), 
            position = position_dodge(width = 0.8),
            min.segment.length = 0)

修改ggplot2中分组条形图中特定条形的颜色。

Created on 2023-06-29 with reprex v2.0.2

英文:

I typically use the gghighlight package for this type of task, e.g.

library(tidyverse)
library(gghighlight)

df &lt;- data.frame(mat = c(&quot;Apr-24&quot;, &quot;Nov-24&quot;, &quot;Apr-25&quot;, &quot;Nov-25&quot;, &quot;Apr-26&quot;, &quot;Sep-26&quot;, &quot;Apr-27&quot;),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 &lt;- df %&gt;%
  pivot_longer(cols = -mat,
               names_to = &quot;event&quot;,
               values_to = &quot;total&quot;)

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                    label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                    values = c(&quot;#ff8080&quot;, &quot;#00cccc&quot;)) +
  gghighlight(mat == &quot;Nov-24&quot;)
#&gt; label_key: mat

修改ggplot2中分组条形图中特定条形的颜色。<!-- -->


If you use the 'darker colours' for all of the bars, then replace the unhighlighted bars with your 'regular colours', you can get your desired outcome:

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                    label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                    values = c(&quot;#870017&quot;, &quot;#3E4EC8&quot;)) +
  gghighlight(mat == &quot;Nov-24&quot;,
              unhighlighted_params = list(fill = rep(c(&quot;#ff8080&quot;, &quot;#00cccc&quot;),
                                                     length(unique(df2$mat)))))
#&gt; label_key: mat

修改ggplot2中分组条形图中特定条形的颜色。<!-- -->

<sup>Created on 2023-06-29 with reprex v2.0.2</sup>


A potential downside to this approach is the legend is lost. One potential workaround for this is to label the bars of interest, e.g.

library(tidyverse)
library(gghighlight)
library(ggrepel)

df &lt;- data.frame(mat = c(&quot;Apr-24&quot;, &quot;Nov-24&quot;, &quot;Apr-25&quot;, &quot;Nov-25&quot;, &quot;Apr-26&quot;, &quot;Sep-26&quot;, &quot;Apr-27&quot;),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 &lt;- df %&gt;%
  pivot_longer(cols = -mat,
               names_to = &quot;event&quot;,
               values_to = &quot;total&quot;)

ggplot(df2, aes(x=mat, y=total, fill=event)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;), 
                    label = c(&quot;TF&quot;, &quot;ZC&quot;), 
                    values = c(&quot;#ff8080&quot;, &quot;#00cccc&quot;)) +
  gghighlight(mat == &quot;Nov-24&quot;) +
  geom_text_repel(aes(label = event), 
            position = position_dodge(width = 0.8),
            min.segment.length = 0)
#&gt; label_key: mat

修改ggplot2中分组条形图中特定条形的颜色。<!-- -->

<sup>Created on 2023-06-29 with reprex v2.0.2</sup>

答案2

得分: 1

另一种方法是创建一个新变量,区分感兴趣的元素,并用其自己的填充颜色突出显示。如果需要,突出显示的元素可以包括在图例中。

library(ggplot2)
library(tidyr)
library(dplyr)

df1 <- data.frame(mat = c("Apr-24", "Nov-24", "Apr-25", "Nov-25", "Apr-26", "Sep-26", "Apr-27"),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 <- 
  df1  |> 
  pivot_longer(cols = -mat,
               names_to = "event",
               values_to = "total") |> 
  mutate(event_grp = ifelse(mat == "Nov-24", paste0(event, 1), event))

ggplot(df2, aes(x=mat, y=total, fill=event_grp)) + 
  geom_bar(width = 0.8, stat = "identity", position = 'dodge') +
  theme_bw() +
  scale_fill_manual(name = c("Method"),
                    breaks = c("term_rv", "zc_rv", "term_rv1", "zc_rv1"),
                    label = c("TF", "ZC", "TF*", "ZC*"), 
                    values = c("#ff8080", "#00cccc", "#870017", "#3E4EC8")) +
  theme(legend.position = "bottom")

修改ggplot2中分组条形图中特定条形的颜色。

Created on 2023-06-29 with reprex v2.0.2

英文:

An alternative approach is to create a new variable distinguishing the elements of interest and highlighting them with their own fill colours. The highlighted elements can be included in the legend if required.

library(ggplot2)
library(tidyr)
library(dplyr)

df1 &lt;- data.frame(mat = c(&quot;Apr-24&quot;, &quot;Nov-24&quot;, &quot;Apr-25&quot;, &quot;Nov-25&quot;, &quot;Apr-26&quot;, &quot;Sep-26&quot;, &quot;Apr-27&quot;),
                 term_rv = c(0.14, 0.34, 0.19, 0.22, 0.34, 0.16, 0.27),
                 zc_rv = c(0.12, 0.31, 0.23, 0.27, 0.36, 0.09, 0.29))

df2 &lt;- 
  df1  |&gt; 
  pivot_longer(cols = -mat,
               names_to = &quot;event&quot;,
               values_to = &quot;total&quot;) |&gt; 
  mutate(event_grp = ifelse(mat == &quot;Nov-24&quot;, paste0(event, 1), event))

ggplot(df2, aes(x=mat, y=total, fill=event_grp)) + 
  geom_bar(width = 0.8, stat = &quot;identity&quot;, position = &#39;dodge&#39;) +
  theme_bw() +
  scale_fill_manual(name = c(&quot;Method&quot;),
                    breaks = c(&quot;term_rv&quot;, &quot;zc_rv&quot;, &quot;term_rv1&quot;, &quot;zc_rv1&quot;),
                    label = c(&quot;TF&quot;, &quot;ZC&quot;, &quot;TF*&quot;, &quot;ZC*&quot;), 
                    values = c(&quot;#ff8080&quot;, &quot;#00cccc&quot;, &quot;#870017&quot;, &quot;#3E4EC8&quot;)) +
  theme(legend.position = &quot;bottom&quot;)

修改ggplot2中分组条形图中特定条形的颜色。<!-- -->

<sup>Created on 2023-06-29 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年6月29日 13:56:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578381.html
匿名

发表评论

匿名网友

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

确定