This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Barplot with base point as 0.5

Hi everyone,

I have data with genes with values ranging from 0 to 1. I made a horizontal barplot (like this). I want the middle point should be 0.5 so that bars should go in both direction (like this) (note I want 0.5 to be center point.

Excel can do that easily (as I made this example).

But

Is there a way to do it in ggplot2 or barplot() ?

My code for trial.

Using ggplot2

p<-ggplot(data=data2, aes(x=Gene, y=Ratio, label=Group, color=Group, fill=Group)) +
  geom_bar(width = 0.5,stat="identity",  position=position_dodge(), size=.3)
p+coord_flip()+ theme_classic()

Using Barplot

barplot(data1, beside=TRUE, horiz=TRUE, col=c("red","blue","orange", "green"), xlim=c(0,max(data1)+0.05))

I tried to find a parameter but could not find one.

I would appreciate any help.

Thanks

barplot

2 answers

Modify the code like this

df$Group<-ifelse(df$Ratio>0.5,"A","B")

ggplot(df,aes(x=Genes,y=Ratio,fill=Group))+geom_bar(stat="identity")+coord_flip()

No, it is not giving me the desired output

Show us the error if any. I am assuming that the column name would be Ratio. Also, change x=Genes to x=Gene

There is no error. It does not give me the right plot. It gives me this.

I wanted this

I already changed x=Gene.

I ran into this scenario before, what I did is the following:

x*-1 for any X < my_cutoff

then the trick in ggplot is to

ggplot(df, aes(x=Genes,y=Ratio,fill=Group)) + .... + scale_y_continuous(breaks = seq(....), labels = abs(seq(...))) + coord_flip()

so in short, negative values will ensure the opposite direction, then you need to specify the labels of the axis to hide the negative values

hth

Yes, That I already did with the groups I have.

I subtracted 0.5 from all the data and plotted and relabelled the scale.

But I am looking for a more direct solution.

Can you share :

head(data2)

and

str(data2)
> head(data2)
  Group  Gene      Ratio
1  UnT1 Gene1 0.92655750
2    T1 Gene1 0.56879310
3  UnT2 Gene1 0.96322899
4    T2 Gene1 0.55393228
5  UnT1 Gene2 0.07426348
6    T1 Gene2 0.21016648

> str(data2)
'data.frame':   180 obs. of  3 variables:
 $ Group: Factor w/ 4 levels "UnT1","T1","UnT2",..: 1 2 3 4 1 2 3 4 1 2 ...
 $ Gene : Factor w/ 45 levels "Gene1","Gene2",..: 1 1 1 1 2 2 2 2 3 3 ...
 $ Ratio: num  0.9266 0.5688 0.9632 0.5539 0.0743 ...

There may not be a more direct solution because the creator of ggplot does have some opinions about how plots and particularly axes should look like. In any case, I would recommend you post that question in a forum dedicated to R since this is not a problem specific to bioinformatics (which is the focus of biostars) and the R specialists might be more inclined to dig deeper into that issue. You can find popular R forums here.

Log in to answer this question.