【问题标题】:Error: Must request at least one colour from a hue palette错误:必须从色调调色板中请求至少一种颜色
【发布时间】:2019-11-04 07:51:59
【问题描述】:

我有一些看起来像这样的数据:

<int>   hgnc_symbol
<fctr>  structure
<int>   Promedio_senal
<dbl>   
>8903   MECP2   10225   7.006842    
>3216   CDKL5   10225   7.484454    
>1405   AUTS2   10225   12.801426   
>4254   DAPK1   10225   6.171004    
>12160  RBFOX1  10225   30.756440   
>8903   MECP2   10185   6.595135    
>3216   CDKL5   10185   6.067631    
>1405   AUTS2   10185   11.053545   
>4254   DAPK1   10185   6.222515    
>12160  RBFOX1  10185   25.431652   

原来它不是一个数据表,所以我通过这样做把我变成了一个:

G_OBJ[, structure:=factor(structure, levels = 1:2, labels = c("10225", "10185"))]

setDT(G_OBJ)

我想做一个条形图,其中 x 轴是基因的名称 (hgnc_symbol),y 轴是基因表达 (Promedio_senal) 我有关于两种不同大脑结构的数据,我想用不同的颜色并排显示两种结构的条形图。我用来执行此操作的代码如下:

G_OBJ[, structure:=factor(structure, levels = 1:2, labels = c("10225", "10185"))]

ggplot(G_OBJ, aes(hgnc_symbol, Promedio_senal)) + 
  geom_col(aes(fill=structure), position = "dodge")+
  scale_x_discrete("GEN") +
  scale_y_continuous("EXPRESION") +
  labs(title = "GENES_OBJETIVO")


但是当我运行它时会出现以下错误消息:

错误:必须从色调调色板中请求至少一种颜色。

我认为错误在于我尝试使用结构作为填充颜色的部分,但我不确定;即使是我不知道要改变什么才能使它正确 感谢您的帮助

【问题讨论】:

  • 您能否以dput 的形式分享您的数据样本以获得更好的重现性?使用dput(head(df,n))

标签: r ggplot2


【解决方案1】:

这是一种在定义美学参数时直接使用geom_bar 并将structure 转换为字符(而不是将其转换为因子)的方法。

library(tidyverse)
G_OBJ %>% 
  ggplot(aes(x = hgnc_symbol,
             y = Promedio_senal,
             fill = as.character (structure))) + 
  geom_bar(stat = "identity", position = position_dodge())+
  scale_x_discrete("GEN") +
  scale_y_continuous("EXPRESION") +
  labs(title = "GENES_OBJETIVO") +
  scale_fill_discrete(name = "structure")

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 2019-03-10
    • 2020-03-31
    相关资源
    最近更新 更多