Barplot with base point as 0.5
2
0
Entering edit mode
4.9 years ago
Ankit ▴ 500

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 • 1.3k views
ADD COMMENT
0
Entering edit mode
4.9 years ago

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()
ADD COMMENT
0
Entering edit mode

No, it is not giving me the desired output

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

I wanted this

I already changed x=Gene.

ADD REPLY
0
Entering edit mode
4.9 years ago
H.Hasani ▴ 990

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

ADD COMMENT
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

Can you share :

head(data2)

and

str(data2)
ADD REPLY
0
Entering edit mode
> 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 ...
ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2928 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