Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 错误:必须从色调调色板请求至少一种颜色_R_Ggplot2 - Fatal编程技术网

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

R 错误:必须从色调调色板请求至少一种颜色,r,ggplot2,R,Ggplot2,我有一些数据如下所示: <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

我有一些数据如下所示:

<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   
我想做一个条形图,x轴上是基因的名称(hgnc_符号),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")


但当我运行此命令时,会出现以下错误消息:

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

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

这里有一种方法,在您定义美学参数时直接使用
geom_bar
并将
structure
as.character(而不是将其转换为factor)

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")

您能否将数据样本作为
dput
共享,以提高再现性?使用
dput(头部(df,n))
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")