basic barplot with ggplot: assign specific color to column with specific values
1
0
Entering edit mode
4.2 years ago
camillab. ▴ 160

Hi,

I want to draw a barplot with basic ggplot but I would like to color the bar in the plot (so genes) that have a specific value (e.g. above 1) with a different color from the other. I found this webiste where they show how to do it but I was wondering if there is a simple/faster way to do it without me going through the dataset and check which row has the value, I am interested and then change the name and so on.. since I am planning to do multiple barplots for multiple genes?

or if there is any other similar question that I might have missed on the web that could answer my question would be much appreciated.

here my code to generate a normal barplot only with one sample (just to give an idea):

#define new dataset
d1 <- mouse$`Log2FC p0`
x= mouse$GeneSymbl

#tell what to plot
to_plot <- data.frame(x=x, "Mouse"=d1)
melted<-melt(to_plot, id="x")

#plot
f <- ggplot(melted,aes(x=x,y=value,fill=variable)) + geom_bar(stat="identity", colour="black", size=0.4, width = 0.8, position="dodge", show.legend = T)+
  labs(y="LOG2FC", x = "Genes shared", title ="Apoptotic genes comparion") + scale_fill_manual(values=c("blue"))
f + theme(legend.text=element_text(size=8), legend.title = element_blank(), legend.position="bottom", panel.background = element_blank(), axis.line = element_line(colour = "black"),axis.text.x = element_text(face="plain", color="black", size=9, angle=45, hjust=1), axis.text.y = element_text(face="plain", color="black",  size=10, hjust=1))

I have also tried to assign a new column to "melted" dataset but it's not working at all, it doesn't ever show the bars to be blue (like in the previous example):

 #define new dataset
    d1 <- mouse$`Log2FC p0`
    x= mouse$GeneSymbl

#tell what to plot
to_plot <- data.frame(x=x, "Mouse"=d1)
melted<-melt(to_plot, id="x")
melted[["sign"]] = ifelse(melted[["value"]] <= -1, "positive", "negative")

#plot
f <- ggplot(melted,aes(x=x,y=value,fill=variable)) + geom_bar(stat="identity", colour="black", size=0.4, width = 0.8, position="dodge", show.legend = T)+
  labs(y="LOG2FC", x = "Genes shared", title ="Apoptotic genes comparion") + scale_fill_manual(values = c("positive" = "darkblue", "negative" = "red"))
f + theme(legend.text=element_text(size=8), legend.title = element_blank(), legend.position="bottom", panel.background = element_blank(), axis.line = element_line(colour = "black"),axis.text.x = element_text(face="plain", color="black", size=9, angle=45, hjust=1), axis.text.y = element_text(face="plain", color="black",  size=10, hjust=1))

thank you very much

Camilla

R ggplot barplot • 10k views
ADD COMMENT
1
Entering edit mode
4.2 years ago

I'm not completely sure what you are asking, but all you need to do for most cases is to make a column with factor levels corresponding to what you want colored differently. If you want a more detailed response post some example data for us to work with, and explain better what you want your output to be.

Here's an example using toy data.

df <- data.frame(gene=sprintf("ENS%05d", seq(100, 105, 1)), count=rnorm(6, 1, 0.25))

> df
      gene     count
1 ENS00100 0.9943232
2 ENS00101 0.8794445
3 ENS00102 0.8482693
4 ENS00103 1.2455513
5 ENS00104 1.1781073
6 ENS00105 0.6342954

Create your column with the factor levels.

df <- mutate(df, condition=if_else(count>1, "greater", "less"))

> df
      gene     count condition
1 ENS00100 0.9943232      less
2 ENS00101 0.8794445      less
3 ENS00102 0.8482693      less
4 ENS00103 1.2455513   greater
5 ENS00104 1.1781073   greater
6 ENS00105 0.6342954      less

Use the new factor levels to color the plot.

  ggplot(df, aes(x=gene, y=count, fill=condition)) +
    geom_col() +
    theme(axis.text.x=element_text(angle=45, hjust=1))

enter image description here

ADD COMMENT

Login before adding your answer.

Traffic: 1576 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6