【问题标题】:How to fix a missing assignment of colour to figure in R如何修复 R 中缺少的颜色分配
【发布时间】:2020-04-21 02:38:48
【问题描述】:

我已使用以下代码生成此图:

Cb64k <- c("#7A87A1", "#788D66",
           "#885578", "#FAD09F", "#FF8A9A", "#D157A0", "#BEC459", "#456648", "#0086ED", "#886F4C",
           "#34362D", "#B4A8BD", "#00A6AA", "#452C2C", "#636375", "#A3C8C9", "#FF913F", "#938A81",
           "#575329", "#00FECF", "#B05B6F", "#8CD0FF", "#3B9700", "#04F757", "#C8A1A1", "#1E6E00",
           "#7900D7", "#A77500", "#6367A9", "#A05837", "#6B002C", "#772600", "#D790FF", "#9B9700",
           "#549E79", "#FFF69F", "#201625", "#72418F", "#BC23FF", "#99ADC0", "#3A2465", "#922329",
           "#5B4534", "#FDE8DC", "#404E55", "#0089A3", "#CB7E98", "#A4E804", "#324E72", "#6A3A4C")

Species2 = group$Species
Species2 = Species2[Species2 != "Filler"]

ggplot(group, aes(x = variable, y = value, fill = Species)) +
  geom_bar(position = "fill", stat = "identity") +
  scale_fill_manual(breaks = Species2, values = Cb64k) +
  scale_y_continuous(labels = percent_format()) +
  theme(legend.position = "bottom", text=element_text(size=11),
        axis.text.x = element_text(angle=0, vjust=1)) +
  guides(fill = guide_legend(ncol=5)) +
  facet_grid(rows=vars(Program), scales = "free_x", space = "free_x") +
  ggtitle(opt$gtitle) +
  xlab("Patient ID") + ylab("Relative Abundance")

如您所见,Debaryomyces fabryi 包含在图中(下图中的空白段),但它看起来像分配了透明颜色,因此它并不真正可见。我该如何解决这个问题?

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。

标签: r ggplot2


【解决方案1】:

您应该尝试通过子集数据框来过滤ggplot2::

ggplot(subset(group, Species != "Debaryomyces fabryi"), aes(x = variable, y = value, fill = Species)) +
  geom_bar(position = "fill", stat = "identity") +
  scale_fill_manual(breaks = Species2, values = Cb64k) +
  scale_y_continuous(labels = percent_format()) +
  theme(legend.position = "bottom", text=element_text(size=11),
        axis.text.x = element_text(angle=0, vjust=1)) +
  guides(fill = guide_legend(ncol=5)) +
  facet_grid(rows=vars(Program), scales = "free_x", space = "free_x") +
  ggtitle(opt$gtitle) +
  xlab("Patient ID") + ylab("Relative Abundance")

它回答了你的问题吗?

如果没有,请通过以下链接提供您的数据集的可重现示例:How to make a great R reproducible example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多