r - ggplot geom_bar: Multiple samples with same name, plotted not side by side -


i have dataframe 2 separate results merged one, each gene has 2 control sample (frc190 , frc193) , 3 unknowns. plot in order read it, color grouping each gene.

   sampleid relativecopy loci 1    frc190     2.040265  abr 2    frc193     1.961293  abr 3     fc124     1.828341  abr 4    fcp920     2.016274  abr 5   e-nh021     1.919309  abr 6    frc190     1.973149 aprt 7    frc193     2.027592 aprt 8    fcp604     2.086984 aprt 9    fcp686     2.027592 aprt 10  fcp1130     1.936854 aprt 

what can that, firstly if use code data in order except 2 controls plotted together.

    df <- within(df, sampleid <- factor(     df$sampleid, levels = c('frc190', 'frc193', 'fcp920', 'e-nh021',    'fc124', 'fcp1130', 'fcp604', 'fcp686' )))      ggplot(data = df, aes(x=sampleid,y=relativecopy, fill = loci))+scale_fill_grey() +     geom_bar(stat="identity", position=position_dodge())+ theme_classic() 

enter image description here

the other option tried renaming controls different names (i.e. frc190-1 , frc190-2) overwriting names on axis. code used is

df <- within(df, sampleid <- factor( df$sampleid, levels = c('frc190', 'frc193', 'fcp920', 'e-nh021', 'fc124',"frc190-2",  "frc193-2",'fcp1130', 'fcp604', 'fcp686' )))  ggplot(data = df, aes(x=sampleid,y=relativecopy, fill = loci))+scale_fill_grey()+ geom_bar(stat="identity", position=position_dodge()) + theme_classic() + scale_x_discrete(breaks=c('frc190', 'frc193', 'fcp920', 'e-nh021', 'fc124',"frc190","frc193", 'fcp1130', 'fcp604', 'fcp686' )) 

this solves first problem of grouping of controls , allows correct order, doesn't allow use of same names. enter image description here

to plot in order can use dummy variable x:

p <- ggplot(df, aes(x=1:nrow(df), y=relativecopy, fill=loci)) +     geom_bar(stat="identity", position=position_dodge()) +      theme_classic() + scale_fill_grey() 

and change x labels can use labels argument scale_x_*:

p + scale_x_discrete(labels=df$sampleid, breaks=1:nrow(df), limits=1:nrow(df), name='sampleid') 
  • labels=df$sampleid sets x tick labels
  • breaks=1:nrow(df) says plot 1 tick per bar
  • limits=1:nrow(df) if leave out plot looks little uncentred (the scale goes 11 on x axis rather 10 being nrow(df))
  • name='sampleid' sets label x axis.

enter image description here

you may wish rotate axis labels if run each other.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -