-1

I have this set of data

data<-read.table("http://pastebin.com/raw.php?i=Vb8S91LX", header=TRUE)

When i plot them using ggplot, the X axis subgroup is automatically arranged as GS, i_GS and Nor for each x variable.

the code i use is

ggplot(data, aes(x=gene, y=value,fill=as.factor(group))) + 
   geom_bar(stat='identity', position='dodge') +
   geom_errorbar(aes(ymin=value, ymax=value+std),position=position_dodge(0.9), width=.2) +
   scale_fill_grey() +
   theme_bw() + 
   xlab("gene symbol")+
   ylab("value") + 
   theme(text = element_text(size=20, face="bold"),
   axis.text.x = element_text(vjust=2))

My question: How could i reverse them "i mean the subgroup not the X axis" so that i could get them as Nor, i_GS, GS?

Data

data <- structure(list(gene = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), .Label = c("C3", "C4A", "C4B", 
"C5", "C6", "C7", "C8A", "C8B", "C8G", "C9"), class = "factor"), 
    group = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L), .Label = c("GS", "i_GS", "Nor"), class = "factor"), 
    value = c(149L, 43L, 19L, 0L, 0L, 0L, 0L, 0L, 0L, 15L, 192L, 
    68L, 67L, 51L, 14L, 13L, 0L, 9L, 7L, 41L, 407L, 70L, 71L, 
    170L, 45L, 47L, 19L, 43L, 24L, 125L), std = c(26L, 17L, 33L, 
    0L, 0L, 0L, 0L, 0L, 0L, 1L, 51L, 3L, 5L, 45L, 12L, 11L, 0L, 
    8L, 6L, 29L, 73L, 6L, 6L, 84L, 16L, 30L, 17L, 30L, 10L, 2L
    )), .Names = c("gene", "group", "value", "std"), class = "data.frame", row.names = c(NA, 
-30L))
user20650
  • 24,654
  • 5
  • 56
  • 91
Samehmagd
  • 473
  • 1
  • 5
  • 13
  • This sound like you need to define the levels of your `group` variable see`?factor`, otherwise they are plot in alphabetical order . Have a search on SO about changing the order of variables in ggplot- there are lots of examples – user20650 Jan 27 '15 at 07:15
  • Change the `factor` level of group `data$group <- factor(data$group, levels=c("Nor", "i_GS", "GS"))` and then change `fill=group` in your `ggplot` call – user20650 Jan 27 '15 at 07:43
  • actually this is a better dup: http://stackoverflow.com/questions/3253641/how-to-change-the-order-of-a-discrete-x-scale-in-ggplot – user20650 Jan 27 '15 at 07:46
  • user20650 yep, i saw that one before posting. as i noted in the post i would like to sort the subgroup not the x axis itself,, thanks – Samehmagd Jan 27 '15 at 07:56

1 Answers1

0

Just add this before ggplot():

data$group <- factor(data$group, levels = c("Nor", "i_GS", "GS"))

Idea is that ggplot reads from factors the way they are arranged in data frame. Hence you have to rearrange the factor in the data frame if you want to see them visualized differently.

statespace
  • 1,644
  • 17
  • 25
  • A. Val ,,, Unfortunately, adding scale_x_discrete(limits = c("Nor", "i_GS", "GS")) does not work – Samehmagd Jan 27 '15 at 07:32
  • Well, it was a wild shot before I had a chance to see your code. – statespace Jan 27 '15 at 07:33
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Werner Jan 27 '15 at 07:55
  • @Werner; but this is a solution - what am i missing thanks? – user20650 Jan 27 '15 at 10:30
  • @A.Val. ; ggplot arranges in alphabetical order, i think, rather than the order they are found in the dataframe – user20650 Jan 27 '15 at 10:33
  • @A.Val , ggplot arranged subgroups different than shown in the data frame . However, setting the levels before plotting did reversed the graph as i need. Thanks ! – Samehmagd Jan 27 '15 at 23:09