【问题标题】:Problems with making scale_colour_manual work for my multi-factor bargraph on ggplot2 in R使 scale_colour_manual 为我在 R 中的 ggplot2 上的多因素条形图工作的问题
【发布时间】:2020-06-08 06:22:08
【问题描述】:

我正在尝试使用 plyr 为我的条形图制作手动色标来汇总数据并使用 ggplot2 来呈现图表。

数据有两个变量:

  1. 区域(显示在 X 轴上)
  2. 基因型(由填充显示)

我已经设法做到了,但是,我还没有找到一种方法来个性化颜色 - 它只是给了我两种随机分配的颜色。

有人可以帮我弄清楚我在这里缺少什么吗?

我已经包含了我的代码和下图的图像。该图表基本上具有我想要的外观,只是我无法个性化颜色。

ggplotdata <- summarySE(data, measurevar="Density", groupvars=c("Genotype", "Region"))
ggplotdata
#Plot the data
ggplotdata$Genotype <- factor(ggplotdata$Genotype, c("WT","KO"))
Mygraph <-ggplot(ggplotdata, aes(x=Region, y=Density, fill=Genotype)) + 
     geom_bar(position=position_dodge(), stat="identity",
           colour="black",
           size=.2) +
     geom_errorbar(aes(ymin=Density-se, ymax=Density+se),
           width=.2,
           position=position_dodge(.9)) +
     xlab(NULL) +
     ylab("Density (cells/mm2)") +
     scale_colour_manual(name=NULL,
           breaks=c("KO", "WT"),
           labels=c("KO", "WT"),
           values=c("#FFFFFF", "#3366FF")) +
     ggtitle("X") +
     scale_y_continuous(breaks=0:17*500) +
     theme_minimal()
Mygraph

【问题讨论】:

  • 当您在aes 中使用fill 时,您必须使用scale_fill_manual 而不是scale_color_manual
  • 感谢您的帮助!现在可以了!
  • @Liptongal 如果您已经回答了您的问题,请记得添加回复并标记为已解决。所以以后如果有人遇到同样的问题,他就能找到答案。
  • @GermanQuinteros 我该怎么做?抱歉,这是我的第一个问题……我只能看到 dc 的回复旁边的标志图标……
  • @Liptongal 添加答案。之后在答案的左上方,如果单击它,您会看到一个灰色的勾号,它将变为绿色。

标签: r ggplot2 colors geom-bar


【解决方案1】:

这里的答案是改用 scale_fill_manual,谢谢@dc37

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-04
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    相关资源
    最近更新 更多