修改图例 – 在使用颜色和透明度时的情况下

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

ggplot - modifying the legend in case when using color and alpha

问题

考虑以下图表:

ggplot(ToothGrowth, aes(x = len, 
                       color = factor(dose),
                       fill = factor(dose),
                       alpha = supp,
                       group = interaction(factor(dose), supp))) + 
  geom_density()

首先,图例似乎有点问题,因为它显示了supp中的两个值的相同alpha值。然而,这不是很重要。

我希望图例看起来像这样(在画图软件中创建):

请注意,我在这个手绘(用鼠标绘制的)图像中使用了空框作为低alpha的替代,实心框表示高alpha。

有人有任何关于如何处理这个问题的想法,或者如何可能编写代码来实现它吗?

我对图例中文本部分的放置没有强烈的意见。我主要关心的是拥有这个由六个彩色框组成的矩阵。

英文:

Consider the following plot:

ggplot(ToothGrowth, aes(x = len, 
                   color = factor(dose),
                   fill = factor(dose),
                   alpha = supp,
                   group = interaction(factor(dose), supp))) + 
  geom_density()

修改图例 – 在使用颜色和透明度时的情况下

First thing, the legend seems a bit wrong because it displays the same value of alpha for both values in supp. However, that's not so important.

I would like the legend to look like this (made in Paint):

修改图例 – 在使用颜色和透明度时的情况下

Note that I used the empty box as a substitute for low alpha in this hand-drawn (mouse-drawn) image, and the filled box represents high alpha.

Does anyone have any idea on how to even approach this, or how would it be possible to code it?

I don't have strong feelings about the placement of the text parts in the legend. My central interest is to have this matrix of six colored boxes.

答案1

得分: 1

这是一个方法,我创建一个用于图例的绘图,然后将其合并进来。

legend_plot <- ggplot(data.frame(dose = factor(c(0.5, 1, 2)),
                  supp = rep(c("OJ", "VC"), each = 3)), 
       aes(x = supp, y = forcats::fct_rev(dose), 
                        color = factor(dose),
                        fill = factor(dose),
                        alpha = supp,
                        group = interaction(factor(dose), supp))) + 
  geom_tile(linewidth = 1, height = 0.8, width = 0.8) + 
  scale_x_discrete(position = "top") +
  guides(color = "none", fill = "none", alpha = "none") +
  labs(y = "dose") +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank())

library(patchwork)

ggplot(ToothGrowth, aes(x = len, 
                        color = factor(dose),
                        fill = factor(dose),
                        alpha = supp,
                        group = interaction(factor(dose), supp))) + 
  geom_density() +
  guides(color = "none", fill = "none", alpha = "none") +
  legend_plot +
  plot_layout(design = "
              1#
              12
              1#", widths = c(3,1))
英文:

Here's an approach where I make a plot for the legend and combine it in.

修改图例 – 在使用颜色和透明度时的情况下

legend_plot &lt;- ggplot(data.frame(dose = factor(c(0.5, 1, 2)),
                  supp = rep(c(&quot;OJ&quot;, &quot;VC&quot;), each = 3)), 
       aes(x = supp, y = forcats::fct_rev(dose), 
                        color = factor(dose),
                        fill = factor(dose),
                        alpha = supp,
                        group = interaction(factor(dose), supp))) + 
  geom_tile(linewidth = 1, height = 0.8, width = 0.8) + 
  scale_x_discrete(position = &quot;top&quot;) +
  guides(color = &quot;none&quot;, fill = &quot;none&quot;, alpha = &quot;none&quot;) +
  labs(y = &quot;dose&quot;) +
  theme_minimal(base_size = 16) +
  theme(panel.grid = element_blank())

library(patchwork)

ggplot(ToothGrowth, aes(x = len, 
                        color = factor(dose),
                        fill = factor(dose),
                        alpha = supp,
                        group = interaction(factor(dose), supp))) + 
  geom_density() +
  guides(color = &quot;none&quot;, fill = &quot;none&quot;, alpha = &quot;none&quot;) +
  legend_plot +
  plot_layout(design = &quot;
              1#
              12
              1#&quot;, widths = c(3,1))

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

发表评论

匿名网友

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

确定