0

Day number is not in order

I used ggplot to plot this data. But I do not know why the day number do not have a ascending order (0-57). Here is my code:

ggplot(data_ready_2, aes(x = Day, y= Chao1, colour=Group)) + geom_line(aes(group=Group), size=1) + geom_point(size=3) +
labs(title = "Chao1") + scale_y_continuous(breaks=seq(0, 30000, 5000))

I tried add this following code;

scale_x_discrete(labels=c("0","2", "4", "6", "8", "15", "22", "29", "43", "57")

it did change the order of Day but did not change the corresponding value on y axis. Please let me know how to fix it. Thanks in advance.

New graph but the spacing is not equal

kelvinfrog
  • 435
  • 1
  • 8
  • 18
  • 1
    Please add output of `dput(data_ready_2)` to your post. I am guessing `Day` is unordered factor. – zx8754 Mar 23 '15 at 21:11
  • 3
    Lexical ordering of factor levels. (As always with R plotting routines.) Whether this can be accomplished with scale_x_continous will depend on your `data_ready_2` object. – IRTFM Mar 23 '15 at 21:12
  • 2
    `data_ready_2$Day = as.numeric(as.character(data_ready_2$Day))` will probably do the trick. – Pewi Mar 23 '15 at 21:19
  • Please see the graph below. The problem now is the spacing between days also changed. – kelvinfrog Mar 23 '15 at 22:14
  • FYI, when I do class(data_ready_2), it returns [1] "data.table" "data.frame". – kelvinfrog Mar 23 '15 at 22:17
  • I tried both scale_x_continous("Day") and data_ready_2$Day = as.numeric(as.character(data_ready_2$Day)). It does do the trick but the spacing between days also changed (see addition graph). How can I keep the spacing between days equal? Thanks. – kelvinfrog Mar 24 '15 at 13:45
  • 1
    `scale_x_discrete(limits=c("0","2", "4", "6", "8", "15", "22", "29", "43", "57")` – LyzandeR Mar 24 '15 at 14:22
  • @Pewi Got a couple questions for you. First, why do you use as.numeric(as.character(data_ready_2$Day)) instead of as.numeric(data_ready_2$Day)? what is the purpose of as.character? Second, how can I create an new object that values in Day column is numeric? My purpose is to keep the data_ready_2 format unchanged. Thanks! – kelvinfrog Mar 26 '15 at 19:01
  • 1
    @kelvinfrog using `as.numeric(myfactor)` generally has lots of undesirable side effects. Please read http://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-an-integer-numeric-without-a-loss-of-information As for your other question: `data_ready_2$Day1 = as.numeric(as.character(data_ready_2$Day))` ? – Pewi Mar 27 '15 at 15:21
  • @Pewi Thanks. May be I did not explain well. My question is how to create a new data frame or data table (e.g. data_ready_3) with the Day column being numeric. The main purpose is to keep data_ready_2 unchanged. I think 'data_ready_2$Day1 = as.numeric(as.character(data_ready_2$Day))' will create a new column (Day1) to data_ready_2, right? – kelvinfrog Mar 27 '15 at 15:58
  • 1
    Yes it will: How about `data_copy = data_ready_2` followed by `data_copy$Day = as.numeric(as.character(data_copy$Day))` – Pewi Mar 27 '15 at 16:05

0 Answers0