2

I have a dataframe like this one below:

df = read.table(text = 
   "names    x       y       MV
   O4,     -0,33    -1,2    -5,2
   O9,5    -0,305   -1,1    -3,6
   B0      -0,3     -1,08   -3,25
   B0,5    -0,28    -1      -2,6
   B1,5    -0,25    -0,9    -2,1
   B2,5    -0,22    -0,8    -1,5", 
   dec = ",", sep = "", header = TRUE)

When plotting with ggplot2, it orders my values based on X axis.

But I wish ggplot to order the points by their positions in the data.frame.

Can someone help me?

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Tiago Bruno
  • 413
  • 1
  • 3
  • 17

1 Answers1

1

Just convert your x column into factor and plot it like this:

df$x <- as.factor(df$x)

library(ggplot2)
#the important thing below is to specify group=1 so that all points
#are treated as the same group. the group argument is needed when
#the x-axis is of class factor.
ggplot(df, aes(x=as.factor(x), y=y, group=1)) + geom_path()

enter image description here

As you can see the x-axis values have the same order as in your data.frame.

LyzandeR
  • 37,047
  • 12
  • 77
  • 87
  • 1
    Thank you very much. There is some method that I could make the negative y axis in the top and positive numbers in bottom? – Tiago Bruno May 10 '15 at 23:51
  • 1
    You are welcome :). Again the only way is to convert your y-axis into factor as well and have your data.frame ordered in the way you like. keep in mind however that if both x-axis and y-axis are factors then your line will always be straight because with both axes being factors you will lose the numeric distances between them. i.e. the distance between 1 and 2 will be the same as 1 and 3 since you got factors and not numbers. – LyzandeR May 10 '15 at 23:55
  • Unless you mean have them ordered as numbers but the other way round (i.e. reversed), in which case you can add `+ scale_y_reverse()` in your plot. – LyzandeR May 10 '15 at 23:57
  • Try this: `ggplot(df, aes(x=as.factor(x), y=y, group=1)) + geom_path() + scale_y_reverse() ` – LyzandeR May 10 '15 at 23:58
  • I just realized that both, Y and X were factors already using scale_y_reverse() I get an error: Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale. must I convert Y to numeric? – Tiago Bruno May 11 '15 at 00:13
  • This means that you already have a `scale_y_` function in your code. You can only have one, otherwise the last one will replace all others. Just transfer the arguments of your initial `scale_y_` into your `scale_y_reverse`. – LyzandeR May 11 '15 at 00:17
  • See this curious stuff typing as.numeric(df$x) I got [1] 10 9 8 7 6 5 4 3 2 1 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 and this is exactly the order my data was plotted on graph, the tenth point was the first the first point was the tenth – Tiago Bruno May 11 '15 at 00:19
  • Also, I suggest if you have more questions to ask them separately as a new question because it is difficult to troubleshoot an error without looking at your code. It might be easier for you to post a different question having a reproducible example (i.e. posting some data and your code that doesnt work). – LyzandeR May 11 '15 at 00:19
  • If the data is numeric it will be plotted in order. Did you use scale_y_reverse? it is very difficult to understand what the problem is without seeing the code..... You are talking about 10 points when you show me 30. – LyzandeR May 11 '15 at 00:21