1

I'm trying to produce a simple ggplot2 jitter plot in R. For x values, I have integers between 1 and 100. When I plot this data my x axis start with a value of 1 then goes to 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, etc. How can I reorder my x axis to create ascending values (eg. 1, 2, 3, 4, 5, ... etc.)?

Here is my code:

data = read.csv('my_data_file', sep"\t")
x_values = data[,1]
y_values = data[,2]  
qplot(rank, score, geom="jitter")  

EDIT: I should have pointed out that I also have a few non-integer x values. Ideally, I would like the x axis to ascend by number and then have the non-integer values appended to the end of the x axis. Any order for the non-integer values is fine.

drbunsen
  • 10,139
  • 21
  • 66
  • 94
  • possible duplicate of [Order Bars in ggplot2 bar graph](http://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph). Or maybe not since this is continuous vs. barchart. – Ari B. Friedman Nov 14 '11 at 17:52

2 Answers2

3

You have to convert to numeric, as @gsk3 says, but as this answer points out, there are some difficulties, and you should use:

x_values <- as.numeric(levels(x_values))[x_values]
Community
  • 1
  • 1
ROLO
  • 4,183
  • 25
  • 41
  • Thanks, this almost worked. I should have mentioned in my original question that I also have two 'non-integer' x-values. When I try this solution I get a warning message: `NAs introduced by coercion`. What is the best way to fix this? – drbunsen Nov 14 '11 at 18:12
  • You would need to get rid of those beforehand. Assuming you want to assign a numeric x value to them you could use car::recode like this: `x_values <- recode(x_values,recodes="'non-integer x1'='10';'non-integer x2'='11'")` (although I'm not sure this is the most efficient way) – ROLO Nov 14 '11 at 18:41
  • 1
    @ROLO Thanks for pointing out the error. I shouldn't answer questions on my way out the door! :-) – Ari B. Friedman Nov 14 '11 at 19:54
  • @gsk3 You're welcome (I was somewhat amazed having to correct you using an answer you had already responded to yourself, but that explains it ;) ). – ROLO Nov 14 '11 at 20:22
1

Your x variable had quotes around it in the CSV (Excel is notorious for this), which means that R read it in as a factor, which it alphabetizes the levels by default. Fix the levels and you'll fix the ordering. Or better yet, since x seems to naturally want to be an integer, fix your data type:

x_values <- as.integer(as.character(x_values))

Try str(data) to see the data types of your columns. If your data is big there are more efficient ways to convert.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235