Reputation: 13
I have some data that looks like this:
<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
Originally it was not a data table, so I turned i tinto one by doing this :
G_OBJ[, structure:=factor(structure, levels = 1:2, labels = c("10225", "10185"))]
setDT(G_OBJ)
I want to do a bar plot where on the x-axis is the name of the gene (hgnc_symbol) and in the y-axis is the gene expresión (Promedio_senal) I have this data on two different brain structure, and I want to show the bars for both structures side by side with different colors. The code I’m using to do this is the following one:
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")
But when I run this the following error message appears:
Error: Must request at least one colour from a hue palette.
I believe that the error is in the part where I try to use structure as the fill color, but I’m not sure; and even if it is that I don’t know what to change to make it correct I appreciate any help
Upvotes: 1
Views: 4249
Reputation: 121
Here is an approach using geom_bar
and turning structure
as.character (instead of converting it into factor), directly when you define aesthetic parameters.
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")
Upvotes: 1