0

I have a dataframe which is really two separate results merged into one, each gene has two control sample (FRC190 and FRC193) and 3 unknowns. I would like to plot this in the order you read it, with color grouping for 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 I can do is almost that, firstly if I use this code I get the data in the order except the two controls a 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 I tried was renaming the controls to different names (i.e. FRC190-1 and FRC190-2) then overwriting the names on the axis. The code used for that 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 the first problem of the grouping of the controls and allows the correct order, but it doesn't allow the use of the same names. enter image description here

George
  • 903
  • 8
  • 22

1 Answers1

2

To plot in order you can just use a dummy variable for your 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 then to change the x labels you can use the labels argument for scale_x_*:

p + scale_x_discrete(labels=df$SampleID, breaks=1:nrow(df), limits=1:nrow(df), name='SampleID')
  • labels=df$SampleID sets the x tick labels
  • breaks=1:nrow(df) says we to plot one tick per bar
  • limits=1:nrow(df) if you leave this out the plot just looks a little uncentred (the scale goes up to 11 on the x axis rather than 10 being nrow(df))
  • name='SampleID' just sets the label for the x axis.

enter image description here

You may also wish to rotate your axis labels if they run into each other.

Community
  • 1
  • 1
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194